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.  [ 4 posts ] 
Author Message
 Post subject: Persistent objects being saved before calling .Save(object)
PostPosted: Mon Jul 17, 2006 10:07 pm 
Beginner
Beginner

Joined: Fri Jul 14, 2006 1:51 pm
Posts: 27
We are using an nHibernate session management utility similar to the one in the Mcaffrey NHibernate Best Practices article.

We have a persistent object (A) that has a collection of persistent objects (B). Object A has a complex validation process it must pass in order to save. But if we create a new B, add it to A and pass A to the validation process and it fails and throws an exception, B is still saved. (.Save(B) is never called)

Is this because the session-in-view pattern is closing the session after the exception is thrown and the request completes and flushes the session? If so, what is the best way to handle this?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jul 17, 2006 10:28 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
When this sort of thing happens to me, it's usually because my validation process performs a query on the A object. This is a very common situation: if you want to validate A, you often need to know about other things in the database that refer to A. The problem is that when hibernate notices that you're querying relative to A, and A is dirty in the session, it'll flush A back to the DB so that the query works with the most up-to-date data.

The way to handle this is to start your save transaction before starting your validation. That way, when validation fails, you can rollback and undo the auto-flushed changes.

_________________
Code tags are your friend. Know them and use them.


Top
 Profile  
 
 Post subject: begin save transaction before validation
PostPosted: Mon Jul 17, 2006 10:48 pm 
Beginner
Beginner

Joined: Fri Jul 14, 2006 1:51 pm
Posts: 27
Thanks for the quick response. I'll give that a try in the morning.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 18, 2006 1:27 pm 
Pro
Pro

Joined: Fri Nov 19, 2004 5:52 pm
Posts: 232
Location: Chicago, IL
I'm not sure if this is the best way to do it or not, but, you can also change the value of ISession.FlushMode. The default is Auto which flushes before queries. If you set it to Commit, it will only flush upon ITransaction.Commit().


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