解决异常org.hibernate.exception.ConstraintViolationException: Could not execute JDBC

测试多对多关联时报错:
org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:94)
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:266)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:171)
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:321)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:50)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1028)
at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:366)
at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:137)
at com.hibernate.manytomany.test.manytomany.add(manytomany.java:83)
at com.hibernate.manytomany.test.manytomany.main(manytomany.java:21)
Caused by: java.sql.BatchUpdateException: ORA-02291: integrity constraint (NM.FK2E2EF2DED58A35C3) violated - parent key not found

at oracle.jdbc.dbaccess.DBError.throwBatchUpdateException(DBError.java:458)
at oracle.jdbc.driver.OraclePreparedStatement.executeBatch(OraclePreparedStatement.java:3907)
at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:70)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:268)
...

问题原因:由于类配置文件关联出现问题。

解决方案:修该类配置文件关联参数。

错误:
<hibernate-mapping package="com.hibernate.manytomany.domain">
	<class name="Teacher">
		<id name="id">
			<generator class="native"></generator>
		</id>
		<property name="name"></property>
		<set name="students" table="teacher_student">
			<key column="teacher_id"></key>
			<many-to-many class="Teacher" column="student_id"></many-to-many>
		</set>
	</class>
</hibernate-mapping>

<hibernate-mapping package="com.hibernate.manytomany.domain">
	<class name="Student">
		<id name="id">
			<generator class="native"></generator>
		</id>
		<property name="name"></property>
		<set name="teachers" table="teacher_student">
			<key column="student_id"></key>
			<many-to-many class="Teacher" column="teacher_id"></many-to-many>
		</set>
	</class>
</hibernate-mapping>


修改后的:
<hibernate-mapping package="com.hibernate.manytomany.domain">
	<class name="Teacher">
		<id name="id">
			<generator class="native"></generator>
		</id>
		<property name="name"></property>
		<set name="students" table="teacher_student">
			<key column="teacher_id"></key>
			<many-to-many class="Student" column="student_id"></many-to-many>
		</set>
	</class>
</hibernate-mapping>



你可能感兴趣的:(java,oracle,sql,Hibernate,jdbc)