java - org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update [Due to Unique Constraint] -
error: org.hibernate.exception.constraintviolationexception: not execute jdbc batch update
java.sql.batchupdateexception: duplicate entry '24-0-es_reservation_detail' key 'questionid_referenceid_referencetype'
i going save reservation object. reservation object contains collection of reservaitondetails objects , each reservation detail object contains collection of questionanswers objects.
the main problem unique constraint on questionanswer table
unqiue constraint: question_id, reference_id, reference_type.
when system save reservation object: system first save reservation, collection of reservation details , question answers 0 (reference_id). while adding question 0 reference id, system throws exception because unique constraint violated.
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 save collection on reservation details
1. insert reservation......... (reservation id = 1) 2. insert reservation_detail....... (reservation detail id = 1) 3. insert reservation_detail....... (reservation detail id = 2) 4. insert question_answer..... (referece_type='rd' referece_id=0, question_id =1) - reservation detail id = 1 5. insert question_answer..... (referece_type='rd' referece_id=0, question_id =1) - reservation detail id = 1 6. update reservation_detail set reservation_id = ? reservation_detail_id = ? (reservation_id = 1, reservation_detail_id = 1) 7. update reservation_detail set reservation_id = ? reservation_detail_id = ? (reservation_id = 1, reservation_detail_id = 2) 8. update question_answer set reference_id = ? question_answer_id = ? (reference_id = 1 , question_answer_id =1) 9. update question_answer set reference_id = ? question_answer_id = ? (reference_id = 2 , question_answer_id =2)
when system execute point (5) script. system through constraint violation exception.
is there way hibernate update reference_id in question_answer (table) before creating next insert query.
script 8 must run after 4th script
script 9 must run after 5th script
hiberate cascade
option insert child objects foreign key if have correctly mapped parent class in child mapping. if have not mapped parent class in child mapping integer
reference_id
, foreign key updated using separate update query:
update question_answer set reference_id = ? question_answer_id = ?
if cannot specify parent class object in child mapping, can use workaround. can set reference_id
null
allowed (on table) , need set referenceid=null;
in child object. hibernate cascade insert child objects null
foreign key, , next call update query set referenceid
generated foreign key.
note: null
values in unique column not considered duplicate values if appeared more 1 time.
Comments
Post a Comment