您好我在尝试更改sql容器中新添加项的属性时遇到SQLIntegrityConstraintViolationException。我有一张非常简单的表格:
CREATE TABLE person(
id int not null primary key,
name varchar(30),
surname varchar(30),
age int
)
我正在尝试逐个设置所有属性,但在第一次完成(id)之后,下一个最终会抛出一个exeption:
java.sql.SQLIntegrityConstraintViolationException: integrity constraint violation: unique constraint or index violation; SYS_PK_10096 table: PERSON
at org.hsqldb.jdbc.JDBCUtil.sqlException(Unknown Source)
at org.hsqldb.jdbc.JDBCUtil.sqlException(Unknown Source)
at org.hsqldb.jdbc.JDBCPreparedStatement.fetchResult(Unknown Source)
at org.hsqldb.jdbc.JDBCPreparedStatement.executeUpdate(Unknown Source)
at com.vaadin.data.util.sqlcontainer.query.TableQuery.executeUpdateReturnKeys(TableQuery.java:588)
at com.vaadin.data.util.sqlcontainer.query.TableQuery.storeRow(TableQuery.java:284)
at com.vaadin.data.util.sqlcontainer.SQLContainer.itemChangeNotification(SQLContainer.java:1069)
at com.vaadin.data.util.sqlcontainer.ColumnProperty.setValue(ColumnProperty.java:207)
at slawek.MyVaadinUI.init(MyVaadinUI.java:93)
at com.vaadin.ui.UI.doInit(UI.java:610)
以下是代码:
FormLayout form = new FormLayout();
TextField idTextField = new TextField("Id");
TextField nameTextField = new TextField("Name");
TextField surnameTextField = new TextField("Surname");
TextField ageTextField = new TextField("Age");
Button submit = new Button("Add user");
form.addComponents(idTextField, nameTextField, surnameTextField, ageTextField, submit);
layout.addComponent(form);
Item item = container.getItem(container.addItem());
container.setAutoCommit(true);
Property propertyId = item.getItemProperty("ID");
Property propertyName = item.getItemProperty("NAME");
Property propertySurname = item.getItemProperty("SURNAME");
Property propertyAge = item.getItemProperty("AGE");
propertyId.setValue(3);
propertyName.setValue("someName"); // line number 93
propertySurname.setValue("someSurname");
propertyAge.setValue(51);
在数据库中我只能看到添加了id号的新行。其余字段为空。 我做错了什么?
答案 0 :(得分:0)
您可以通过将下面的行分成两行来检查情况,并在修改它们之前检查container.addItem()返回的对象的字段。如果object的ID为其余字段为null,则不应使用proertyId.setValue(3),因为已经设置了它。
Item item = container.getItem(container.addItem());