0

我正在将我的应用程序的 grails 版本从 1.3.7 升级到 2.2.4,现在一个精神错误困扰着我自己。

如果我取消 dateCreated 属性工作正常。我尝试使用 beforeInsert,但问题是一样的。

我有一个自引用实体,如下面的小示例:

Class Employee {
    Date dateCreated
    Name name
    Employee brother
}

我使用 xml hibernate 映射文件,这种关系表示如下

<property name="dateCreated" column="dateofcreation" not-null="false"/>
<many-to-one name="brother" column="brother" cascade="save-update" fetch="join"/>

问题是......当我尝试使用 Grails 中的 save(flush: true) 同时保存员工和兄弟时,如下所示

void testSaveEmployeeAndBrother() {
    Employee e1 = new Employee('employee')
    Employee e2 = new Employee('brother')
    e1.brother = e2
    e2.brother = e1
    assert e1.save(flush: true)
}

我有以下错误消息:

Could not execute JDBC batch update; SQL [insert into employee (name, brother, id) values (?, ?, ?)]; constraint [null]; nested exception is org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update
org.springframework.dao.DataIntegrityViolationException: Could not execute JDBC batch update; SQL [insert into employee (name, brother, id) values (?, ?, ?)]; constraint [null]; nested exception is org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update
    at EmployeeTests.testSaveEmployeeAndBrother(EmployeeTests.groovy:22)
Caused by: org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update
    ... 1 more
Caused by: java.sql.BatchUpdateException: Entrada em lote 0 insert insert into employee (name, brother, id) values ('brother', '1', '2') foi abortada. Chame getNextException para ver a causa.
    at org.postgresql.jdbc2.AbstractJdbc2Statement$BatchResultHandler.handleError(AbstractJdbc2Statement.java:2621)
    at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:1837)
    at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:407)
    at org.postgresql.jdbc2.AbstractJdbc2Statement.executeBatch(AbstractJdbc2Statement.java:2754)
    ... 1 more

我使用 grails 2.2.4、jdk-1.7.0_40、postgres 9.2

4

0 回答 0