我有实体到实体的one to many
关联。Student
Subject
Student
实体关联映射是
<class name="Student" table="STUDENT">
<id name="studentId" column="STUD_ID">
<generator class="increment"/>
</id>
<property name="studentName" column="STUD_NAME"/>
<list access="field" lazy="false" name="subjects" cascade="all-delete-orphan" fetch="subselect">
<key column="STUD_ID" />
<list-index column="SUBJECT_ORDER_ID" />
<one-to-many class="com.sample.Subject" />
</list>
</class>
主题表映射是
<class name="Subject" table="SUBJECT">
<id name="subjectId" column="SUB_ID">
<generator class="increment"/>
</id>
<property name="subjectName" column="SUB_NAME"/>
</class>
我在表中有一个表级约束,SUBJECT
并且STUDENT_ID
不SUBJECT_ORDER_ID
应该是null
。
ALTER TABLE SUBJECT ADD CONSTRAINT CHK_FIELD CHECK(STUD_ID IS NOT NULL AND SUBJECT_ORDER_ID IS NOT NULL)
保存这些条目的主类
public static void main(String[] args) {
Session session = HibernateHelper.getSessionFactory().getCurrentSession();
session.beginTransaction();
Subject subject = new Subject();
subject.setSubjectName("Test Subject2");
// session.save(subject);
List<Subject> subjects = new ArrayList<Subject>();
subjects.add(subject);
Student student = new Student();
student.setStudentName("Test student2");
student.setSubjects(subjects);
session.save(student);
session.getTransaction().commit();
}
当Student
实体被创建时关联Subject
。Hibernate 正在尝试Subject
使用空创建实体,STUD_ID
然后SUBJCT_ORDER_ID
再分配映射。因此,Subject
由于违反约束,实体创建失败。异常日志是
Hibernate: select max(STUD_ID) from STUDENT
Hibernate: select max(SUB_ID) from SUBJECT
Hibernate: insert into STUDENT (STUD_NAME, STUD_ID) values (?, ?)
Hibernate: insert into SUBJECT (SUB_NAME, SUB_ID) values (?, ?)
2012-06-07 17:26:52,484 [main] ERROR org.hibernate.util.JDBCExceptionReporter - Check constraint violation: "CHK_FIELD: ((STUD_ID IS NOT NULL)
AND (SUBJECT_ORDER_ID IS NOT NULL))"; SQL statement:
insert into SUBJECT (SUB_NAME, SUB_ID) values (?, ?) [23513-164]
2012-06-07 17:26:52,484 [main] ERROR org.hibernate.util.JDBCExceptionReporter - Check constraint violation: "CHK_FIELD: ((STUD_ID IS NOT NULL)
AND (SUBJECT_ORDER_ID IS NOT NULL))"; SQL statement:
insert into SUBJECT (SUB_NAME, SUB_ID) values (?, ?) [23513-164]
Exception in thread "main" org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:96)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:275)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:268)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:184)
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:321)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:51)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1216)
at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:383)
at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:133)
at com.sample.TestMain.main(TestMain.java:27)
Caused by: org.h2.jdbc.JdbcBatchUpdateException: Check constraint violation: "CHK_FIELD: ((STUD_ID IS NOT NULL)
AND (SUBJECT_ORDER_ID IS NOT NULL))"; SQL statement:
insert into SUBJECT (SUB_NAME, SUB_ID) values (?, ?) [23513-164]
at org.h2.jdbc.JdbcPreparedStatement.executeBatch(JdbcPreparedStatement.java:1107)
at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:70)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:268)
... 8 more
当我禁用约束 CHK_FIELD:STUD_ID
并且SUBJECT_ORDER_ID
不应该为空时,两个实体都成功创建。请帮我弄清楚我是否错过了任何配置?