0

我有实体到实体的one to many关联。StudentSubject

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_IDSUBJECT_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 不应该为空时,两个实体都成功创建。请帮我弄清楚我是否错过了任何配置?

4

2 回答 2

0

使用注释你可以这样做 -

@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
@JoinColumn(name = "STUDENT_ID",nullable=false)
@ForeignKey(name = "fk_student")     
private List<Subject> subjects= new ArrayList<Subject>();      
于 2012-06-07T10:41:47.927 回答
0

此实体创建 Student 和 Subject 分三步完成

  1. 创建学生实体
  2. 创建所有主题实体
  3. 在所有主题实体中设置 STUD_ID

由于 Subject 实体有约束 STUD_ID 不应为空。第二步违反了这个约束。

于 2012-06-26T06:26:35.550 回答