-->
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.  [ 1 post ] 
Author Message
 Post subject: Reset object PKs after insert failure and rollback?
PostPosted: Wed Apr 09, 2008 12:18 pm 
Newbie

Joined: Tue Apr 08, 2008 8:01 pm
Posts: 2
When attempting to saveOrUpdate() a number of objects, Hibernate assigns the primary key to the objects as they are inserted.

Unfortunately, if an exception is thrown by, say, the 4th saveOrUpdate() call, the first 3 objects inserted still have their primary key set even after rolling back the transaction. The problem with that is that when a saveOrUpdate is retried for those 3 objects, Hibernate will think they should be updated instead of inserted since the primary key is no longer null.

Is there a way around this problem other than manually nullifying the primary keys of all the objects after rolling back the transaction? Perhaps some call I'm not aware of which will nullify the PK of all saveOrUpdate()ed objects during the transaction?

Thanks for your help.


Hibernate version: 3

Code:
      Transaction tx = s.beginTransaction();
      try {
         s.saveOrUpdate(user);
         Set<Children> children = user.getChildren();
         if (children!=null) {
            Iterator childIterator = children.iterator();
            while (childIterator.hasNext()) {
               s.saveOrUpdate(childIterator.next());
            }
         }
         tx.commit();
      }
      catch (Throwable t) {
         tx.rollback();
         // NEED SOMETHING HERE TO NULLIFY PKs IN EVENT OF MID-SAVE EXCEPTION
         throw new CLockException("An error occurred while saving the user.", t);
      }


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

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.