-->
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: Reattach after delete and rollback
PostPosted: Tue Dec 12, 2006 5:36 am 
Newbie

Joined: Fri Nov 14, 2003 12:09 pm
Posts: 6
Location: Paris
After getting an exception while deleting a bean, can I "reattach" the bean to the same session ?
I try this code

try {
session.delete(bean);
session.flush();
} catch (org.hibernate.exception.ConstraintViolationException e) {
session.getTransaction().rollback();
}

but it seems that the bean stays in "deleted" state in the session.
Further a new flush cause the same exception.
I try the reload the bean but I get a null bean
If I close and reopen the session its OK but if possible I want keep the session.

Thanks for your help.


Top
 Profile  
 
 Post subject: Re: Reattach after delete and rollback
PostPosted: Tue Dec 12, 2006 6:14 am 
Expert
Expert

Joined: Tue Dec 28, 2004 7:02 am
Posts: 573
Location: Toulouse, France
galland wrote:
After getting an exception while deleting a bean, can I "reattach" the bean to the same session ?
[...]
If I close and reopen the session its OK but if possible I want keep the session.


It's not possible. If an exception occured within a session, you must trash this session and create a new one.
See http://www.hibernate.org/hib_docs/v3/re ... tices.html :
Quote:
Don't treat exceptions as recoverable.

This is more of a necessary practice than a "best" practice. When an exception occurs, roll back the Transaction and close the Session. If you don't, Hibernate can't guarantee that in-memory state accurately represents persistent state. As a special case of this, do not use Session.load() to determine if an instance with the given identifier exists on the database; use Session.get() or a query instead.

_________________
Baptiste
PS : please don't forget to give credits below if you found this answer useful :)


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.