I am new to Vaadin, but pretty sure this is a bug. The ID returned by SQLContainer.addItem() cannot be used for a subsequent getItem() call. Maybe this only bites with an “identity” type primary key, but using an identity key is a common pattern, and SQLContainer seems to explicitly try to support it, in general. I believe the problem is related to what is returned by addItem(): the RowId returned does not contain the same value type as what is returned by getItemIds().
Table:
CREATE TABLE bugDemo(
[id]
[int]
IDENTITY(1,1) NOT NULL,
[version]
[int]
NOT NULL default (1),
[someOtherproperty]
varchar(20) null,
CONSTRAINT [PK_bugDemo]
PRIMARY KEY CLUSTERED
(
[id]
ASC
) ON [PRIMARY]
) ON [PRIMARY]
Code to illustrate the problem:
private void showProblem(Layout layout) throws SQLException {
final TableQuery demoTable;
JDBCConnectionPool pool = new SimpleJDBCConnectionPool(
"com.microsoft.sqlserver.jdbc.SQLServerDriver",
"jdbc:sqlserver://localhost:1433;databaseName=vaadinExercise;",
"foo",
"bar"
);
MSSQLGenerator gen = new MSSQLGenerator();
demoTable = new TableQuery("bugDemo", pool, gen);
demoTable.setVersionColumn("version");
SQLContainer container = new SQLContainer(demoTable);
container.setAutoCommit(true);
Object newKey = container.addItem();
Object newKeyValue = ((com.vaadin.data.util.sqlcontainer.RowId)newKey).getId();
layout.addComponent(new Label("added new item, ID=" + newKey + "," + newKeyValue[0]
.getClass() ));
Collection<?> itemIds = container.getItemIds();
Object oneKey = itemIds.toArray()[0]
;
Object oneKeyValue = ((com.vaadin.data.util.sqlcontainer.RowId)oneKey).getId();
layout.addComponent(new Label("but the class of item IDs in the table is " + oneKeyValue.getClass()));
Item addedItem = container.getItem(newKey);
if (null == addedItem) {
layout.addComponent(new Label("Therefore, you cannot retrieve the item you just added!"));
} else
{
layout.addComponent(new Label("But you can retrieve the item by the key, anyway"));
}
}
}
Output:
added new item, ID=4,class java.math.BigDecimal
but the class of item IDs in the table is class [Ljava.lang.Object;
Therefore, you cannot retrieve the item you just added!
I ran into this using Vaadin version 7.4.5, and reproduced it again in version 7.5.0. Using sqljdbc41.jar from Microsoft.
Any advice?