-->
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: ISession.Find bug?
PostPosted: Thu Mar 30, 2006 4:26 pm 
Beginner
Beginner

Joined: Tue May 17, 2005 11:42 am
Posts: 22
I would like to know if anyone has an idea on this:
I do an ISession.Find, that returns only one object, modify a value of the object and then do and an ISession.Flush but without doing a ISession.SaveOrUpdate and ISession.Commit.
Can someone tell me why does the object get persisted to the database?
Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 03, 2006 6:24 am 
Newbie

Joined: Mon Feb 20, 2006 6:31 am
Posts: 17
I think Session.Flush() writes your changes to the database. I think this is one of the nice (but maybe also dangerous) features of NHibernate or ORM in general: you load, do whatever you want to and then flush without having to do an explicit save or update for each separate change!


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 04, 2006 8:17 am 
Newbie

Joined: Fri Jan 20, 2006 12:47 pm
Posts: 16
Location: Ottawa, On
This is one of the biggest differences that I found I had to adjust to going from SQL based db access to nHibernate.

the Session object acts as a 1st level cache, meaning that any object you load from the db is cached within the Session.

So, what you receive as a result from a Load(), or Get() or list() .. etc is a reference to a cached object in the Session. As you make changes to these objects, the session is aware of what changes you are making. These changes are queued up in the session and can be flushed to the db in a variety of ways.

There is a FlushMode property on the Session itself which controls this flushing behaviour. If that is set to Auto (which I believe is the default) the session will flush itself from time to time without you even knowing.

If you have wrapped your code in a Transaction (i.e. Session.BeginTransaction()), when that transaction is Committed a flush will occur.

Of course, you can also force a flush as you have indicated.

Flushing becomes even more complicated as in certain situations (I'm not sure of the exact algorithm), a session will flush before performing a query and conversely, will query before performing a Save.

Sounds a bit like you have no control, but you do. You can set the FlushMode to either Flush on Commit, or Never. Oviously Commit allows you to control when flushing is performed via transactions ... this is what I use and it seems to work well. I also use Never sometimes when performing queries and I do not want the Session doing any extra loading / flushing behind my back.

In your case, if you don't want the object you have changed to be flushed, you can either Close() the session which leaves the object as transient ... you can re-attach it to another session by calling SaveOrUpdate() at a later point in your execution.

You can also use the Evict() method on the session to evict that object from the first level cache and therefore the session will not be able to track any changes made to that object and they will not be flushed.

Hope this helps ....


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.