简体   繁体   English

org.hibernate.exception.ConstraintViolationException:无法执行JDBC批量更新[由于唯一约束]

[英]org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update [Due to Unique Constraint]

Error: org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update 错误: org.hibernate.exception.ConstraintViolationException:无法执行JDBC批处理更新

java.sql.BatchUpdateException: Duplicate entry '24-0-es_reservation_detail' for key 'questionId_referenceId_referenceType' java.sql.BatchUpdateException:关键字'questionId_referenceId_referenceType'的重复条目'24 -0-es_reservation_detail'

I am going to save reservation object. 我要保存预订对象。 This reservation object contains collection of reservaitonDetails objects and each reservation detail object contains collection of questionAnswers Objects. 此预留对象包含reservaitonDetails对象的集合,每个预留详细信息对象包含questionAnswers对象的集合。

The main problem is unique constraint on questionAnswer table 主要问题是对questionAnswer表的唯一约束

Unqiue Constraint: question_id, reference_id, reference_type.

When system save reservation object: system first save reservation, then collection of reservation details and then all its question answers with 0 (reference_id). 当系统保存预约对象时:系统首先保存预约,然后收集预订详细信息,然后用0(reference_id)查询所有问题答案。 While adding question with 0 reference id, system throws exception because unique constraint is violated. 在添加带有0引用id的问题时,系统会抛出异常,因为违反了唯一约束。

ReservationDetail.hbm.xml ReservationDetail.hbm.xml

.............
.............
<set name="questionAnswers" lazy="true" cascade="all-delete-orphan" where="reference_type = 'es_reservation_detail'">
        <key column="reference_id"/>
        <one-to-many class=".....QuestionAnswerDTO" />
    </set>
.............
.............

Example: 例:

If we save collection on reservation details 如果我们保存预订详情的收集

1. insert into reservation......... (reservation id = 1)

2. insert into reservation_detail.......   (reservation detail id = 1)
3. insert into reservation_detail.......   (reservation detail id = 2)

4. insert into question_answer..... (referece_type='RD' referece_id=0, question_id =1) - For Reservation Detail id =  1
5. insert into question_answer..... (referece_type='RD' referece_id=0, question_id =1) - For Reservation Detail id =  1

6. update reservation_detail set reservation_id = ? where reservation_detail_id = ? (reservation_id = 1, reservation_detail_id = 1)
7. update reservation_detail set reservation_id = ? where reservation_detail_id = ? (reservation_id = 1, reservation_detail_id = 2)

8. update question_answer set reference_id = ? where question_answer_id = ? (reference_id = 1 and question_answer_id =1)
9. update question_answer set reference_id = ? where question_answer_id = ? (reference_id = 2 and question_answer_id =2)

When system will execute point (5) script. 当系统执行point(5)脚本时。 System will through constraint violation exception. 系统将通过约束违规异常。

Is there any way that hibernate update reference_id in question_answer (table) immediately before creating next insert query. 有没有什么方法可以在创建下一个插入查询之前立即在question_answer(table)中更新reference_id。

Script 8 must run after 4th script 脚本8必须在第4个脚本之后运行

Script 9 must run after 5th script 脚本9必须在第5脚本之后运行

Most probably you did not define In QuestionAnswerDTO entity a reference back to master entity, which is ReservationDetails. 很可能你没有在QuestionAnswerDTO实体中定义一个返回主实体的引用,即ReservationDetails。 You should configure a @MAnyToOne in QuestionAnswerDTO, that has as @JoinColumn "reference_id" something like that 您应该在QuestionAnswerDTO中配置@MAnyToOne,其中@JoinColumn为“reference_id”,类似于

@ManyToOne
 @JoinColumn(name="persistence_id")
 private ReservationDetail reservationDetail;

and then set reservationDetail property to the actual ReservationDetail object to which it belongs. 然后将reservationDetail属性设置为它所属的实际ReservationDetail对象。

Then JPA will know that it needs to populate reference_id with a reference to REservationDetails. 然后JPA将知道它需要使用对REservationDetails的引用来填充reference_id。 Otherwise it considers it as any other field 否则它将其视为任何其他字段

Hiberate cascade option insert child objects with foreign key if you have correctly mapped Parent Class in child mapping. 如果您已在子映射中正确映射父类,则Hiberate cascade选项会使用外键插入子对象。 If you have not mapped Parent Class in child mapping but as Integer reference_id , then foreign key will be updated using separate update query: 如果您没有在子映射中映射父类,而是将其映射为Integer reference_id ,则将使用单独的更新查询更新外键:

 update question_answer set reference_id = ? where question_answer_id = ?

If you cannot specify Parent class object in Child mapping, then you can use a workaround. 如果您不能在子映射中指定父类对象,则可以使用变通方法。 You can set reference_id as null allowed (on table) and you need to set referenceId=null; 您可以将reference_id设置为null (在表上),并且您需要设置referenceId=null; in child object. 在儿童对象中。 Then Hibernate cascade will insert child objects will null foreign key, and next call update query to set referenceId generated foreign key. 然后Hibernate级联将插入子对象将null外键,然后调用update查询来设置referenceId生成的外键。

Note: null values in unique column are not considered as duplicate values if appeared more than one time. 注意:如果出现多次,则唯一列中的null值不会被视为重复值。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 org.hibernate.exception.ConstraintViolationException:无法执行JDBC批量更新 - org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update org.hibernate.exception.ConstraintViolationException: 无法执行 JDBC 批量更新 - org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update 休眠一对一映射错误org.hibernate.exception.ConstraintViolationException:无法执行JDBC批更新 - Hibernate one to one mapping error org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update 无法执行JDBC批处理更新:线程“ main” org.hibernate.exception.ConstraintViolationException中的异常: - Could not execute JDBC batch update:Exception in thread “main” org.hibernate.exception.ConstraintViolationException: org.hibernate.exception.ConstraintViolationException:保存高级OneToMany替换项时无法执行JDBC批更新 - org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update at saving hibrnate OneToMany reletionship JPA + Hibernate:org.hibernate.exception.ConstraintViolationException:无法执行批处理 - JPA + Hibernate : org.hibernate.exception.ConstraintViolationException: could not execute batch 无法执行语句; SQL [不适用]; 约束 [null]; 嵌套异常是 org.hibernate.exception.ConstraintViolationException: - could not execute statement; SQL [n/a]; constraint [null]; nested exception is org.hibernate.exception.ConstraintViolationException: 无法执行语句; SQL [不适用]; 约束 [PRIMARY]; 嵌套异常是 org.hibernate.exception.ConstraintViolationException - could not execute statement; SQL [n/a]; constraint [PRIMARY]; nested exception is org.hibernate.exception.ConstraintViolationException org.hibernate.exception.ConstraintViolationException:无法执行语句 - org.hibernate.exception.ConstraintViolationException: could not execute statement org.hibernate.exception.ConstraintViolationException:无法执行语句 - org.hibernate.exception.ConstraintViolationException: could not execute statement
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM