-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: Updating children uniquely indexed property in right order
PostPosted: Wed Sep 24, 2003 6:20 am 
Newbie

Joined: Wed Sep 24, 2003 5:45 am
Posts: 1
The case:
there is Many-to-One relation.
In parent I'm iterating through set of children updating child property which is part of unique constraint (unique key in table).
The problem:
saving parent in session Hibernate throws exception

java.sql.SQLException: ERROR: Cannot insert a duplicate key into unique index

The problem is that Hibernate should execute SQL's in "right" order.

1. Possible fix No1:
Of course I could call saveOrUpdate() on every child to ensure Hibernate will generate SQL's in right order. But I want parent class, where this logic is, not to know anything about Hibernate and being persisted in third place like parent.saveOrUpdate().

2. Possible fix No2:
Unreference children set from parent, so that Hibernate first DELETE children. That update children set, set children id's to null and assing set back to parent. But it doesn't work, perhaps because Hibernate first processes inserts and updates and only than DELETES. So that error on update is thrown first.

May be anyone knows how to resolve this problem gracefully?
Thanx


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 24, 2003 6:37 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
This is a FAQ item. The solution is to give Hibernate "hints" by calling flush() manually.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.