0

您好我在尝试更改 sql 容器中新添加项目的属性时收到 SQLIntegrityConstraintViolationException。我有一个非常简单的表:

CREATE TABLE person(
id int not null primary key,
name varchar(30),
surname varchar(30),
age int
)

我正在尝试一个一个地设置所有属性,但是在第一次完成(id)之后,下一个最终会抛出一个异常:

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 号的新行。其余字段为空。我究竟做错了什么?

4

1 回答 1

0

您可以通过将下面的行分成两行来检查情况,并在修改之前检查由 container.addItem() 返回的对象的字段。如果对象的 ID 其余字段为空,则不应使用 proertyId.setValue(3),因为它已经设置。

Item item = container.getItem(container.addItem());
于 2013-10-10T09:44:29.777 回答