使用多对多映射在hibernate中更新表

时间:2015-07-25 04:07:18

标签: java hibernate oracle-sqldeveloper

我有两个POJO学位和课程。它们是多对多映射,我使用XML将Degrees映射到课程。创建Degree时,它会插入一个id,degree name,department和Course类的Java Set。

映射如下所示:

<class name="Degree" table="MY_DEGREES">
    <id name="id" column="DEGREE_ID">
        <generator class="increment" />
    </id>

    <property name="degreeName" unique="true"/>
    <property name="department"/>


<set name="degreeCourses"
    table="DEGREE_COURSE"
    cascade="save-update">
    <key column="DEGREE_ID"/>
    <many-to-many class="Course" column="COURSE_ID"/>
</set>

添加一个尚不存在的课程时工作正常但是因为如果我添加一个已经存在的课程名称的Degree,我将课程名称设置为唯一,那么它将为我提供一个JDBC BATCH更新错误并且不会添加表格。

我希望能够在课程表中添加课程(如果课程表中尚不存在),或者在课程表中添加课程更新(如果有)。

非常感谢任何帮助! 先谢谢

编辑:

这是我的课程hbm

<class name="Course" table="MY_COURSES">
    <id name="id" column="COURSE_ID">
        <generator class="increment" />
    </id>

    <property name="courseName" unique="true"/>
    <property name="subject"/>
    <property name="semester" />
    <property name="capacity" />
    <property name="roomNumber"/>
    <property name="buildingName"/>
    <property name="professor"/>
    <property name="courseDescription"/>
    <property name="credits"/>


</class>

这是完整的例外情况。

Hibernate: select max(COURSE_ID) from MY_COURSES
Hibernate: insert into MY_COURSES (courseName, subject, semester, capacity,     roomNumber, buildingName, professor, courseDescription, credits, COURSE_ID)     values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
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(Abstract    FlushingEventListener.java:321)
    at     org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListe    ner.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.jamellawson.www.OracleDAO.saveObject(OracleDAO.java:35)
    at com.jamellawson.www.Degree.main(Degree.java:53)
Caused by: java.sql.BatchUpdateException: ORA-00001: unique constraint     (JL.SYS_C005028) violated

    at     oracle.jdbc.driver.DatabaseError.throwBatchUpdateException(DatabaseError.java:34    3)
    at     oracle.jdbc.driver.OraclePreparedStatement.executeBatch(OraclePreparedStatement.    java:10657)
    at     org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:70)
    at     org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:268)
    ... 9 more

0 个答案:

没有答案