-->
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.  [ 3 posts ] 
Author Message
 Post subject: ID generated regardless of exception (2.1.7)?
PostPosted: Thu Apr 28, 2005 1:20 am 
Newbie

Joined: Thu Jan 06, 2005 8:05 am
Posts: 13
Hibernate version:2.1.7

Hi.

Is it normal that when persisting new object, it's ID gets generated regardless of exception being raised because of some constraint issue?

My application manages presentation differently when dealing with fresh (getId() == null) and already persisted objects (getId() != null).
Problem is that when new object is to be persisted, in some situations some expected db exception is raised, handled as expected exception, but my app is confused because this new object, although hasn't been persisted successfuly, now has getd()!= null , thus my app thinks it's persisted !?

Some suggestion?

-Vjeran


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 28, 2005 4:34 am 
Regular
Regular

Joined: Tue Nov 23, 2004 7:42 am
Posts: 82
Location: London, England
Yes it is normal. Think about it like this.

You are trying to persist an object into the database. It needs an ID so one is genereted before attempting to insert the object. Then, the insert fails due to a constraint error but the ID has already been generated. Databases do not 'roll back' generated IDs when there is an error creating a database entry.

I suggest you rewrite your error handling so that failures are handled safely.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 28, 2005 8:32 am 
Expert
Expert

Joined: Fri Nov 07, 2003 4:24 am
Posts: 315
Location: Cape Town, South Africa
Rilux is 100% on this one. Just one point to remember is that hibernate does not roll back 'object state'. If you catch a (system) exception and wish to continue it may be more prudent to close the current session and open a new one. Of course this depends on what exactly caused the exception.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 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.