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.  [ 8 posts ] 
Author Message
 Post subject: updating obj property, updates database How to avoid this?
PostPosted: Thu Aug 30, 2007 9:28 pm 
Beginner
Beginner

Joined: Wed Aug 29, 2007 1:25 pm
Posts: 26
Location: Recife, PE, Brazil
Hello all,

Please, have a look at this code and tell me what's happening and why.

Code:

//first, i create a businnes object and define its ID
BusinessObject obj = new BusinessObject (id);

// secondly, I use NHibernateSession in order to get that object with the data stored in the database
BusinessObject obj = NHibernateSession.Get(obj);   

obj.Property = "new value";

...
...
...
last code of the page


after I have defined a new value to property it's called other codes of the application, but NHibernateSession  is never called so that to update the new value in the database.

but when I take a look at database the data is updated.

I wouldn't like to leave this behavior...in my case I use NHibernateSession to update explicitly.

So, How to avoid that ?



Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 30, 2007 11:28 pm 
Hibernate Team
Hibernate Team

Joined: Tue Jun 13, 2006 11:29 pm
Posts: 315
Location: Calgary, Alberta, Canada
In the same session, if you load/get an entity, update its property, then flush the session (either explicitly or implicitly). The updates to the entity will get propagated to the database; no explicit call to Update() is needed. If you don't want that behavior, call ISession.Evict() to remove the entity some time before the session is flushed.

_________________
Karl Chu


Top
 Profile  
 
 Post subject: I see...
PostPosted: Fri Aug 31, 2007 9:04 am 
Beginner
Beginner

Joined: Wed Aug 29, 2007 1:25 pm
Posts: 26
Location: Recife, PE, Brazil
I see...

would be it ?
Code:
BusinessObject obj = NHibernateSession.Get(obj);   

obj.Property = "new value";

NHibernateSession.Evict()


that would avoid "BusinessObject" update and all other ones in the Session?

how to "flush" and "evict" just a specif object that is in the section?

thanks!

[/code]


Top
 Profile  
 
 Post subject:
PostPosted: Fri Aug 31, 2007 9:54 am 
Hibernate Team
Hibernate Team

Joined: Tue Jun 13, 2006 11:29 pm
Posts: 315
Location: Calgary, Alberta, Canada
You pass the object to be evicted to Evict() like this:

Code:
NHIbernateSession.Evict(obj);

_________________
Karl Chu


Top
 Profile  
 
 Post subject:
PostPosted: Fri Aug 31, 2007 10:06 am 
Beginner
Beginner

Joined: Wed Aug 29, 2007 1:25 pm
Posts: 26
Location: Recife, PE, Brazil
THANKS a million.

I didn't know that method.

Chapter 15.3. Managing the caches tells all what I was needing

just one more think..

do you think the synchronization by default wouldn't an overhead to NHibernate?

Imagine you're in a presentation layer...where no update will propagate to database...I think it's a good idea evict completely all objects from the session cache ( or a few of them)


Top
 Profile  
 
 Post subject:
PostPosted: Fri Aug 31, 2007 10:24 am 
Hibernate Team
Hibernate Team

Joined: Tue Jun 13, 2006 11:29 pm
Posts: 315
Location: Calgary, Alberta, Canada
When the session is closed, the entities cached in it are correspondingly discarded. If you follow the recommendation of making sure a session is short-lived, it generally would not be a concern.

If you want to have (or fake) a read-only session, you can set the FlushMode of the session to Never, and make sure you don't call Flush() before you close the session.

_________________
Karl Chu


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 13, 2007 8:38 pm 
Regular
Regular

Joined: Fri Jan 20, 2006 7:45 pm
Posts: 97
Location: San Antonio, TX
You could also set FlushMode to commit after opening the session.
session.FlushMode = FlushMode.Commit;

That way you will only have flushes on Commits. karlchu is right though, you need short lifespans for your sessions, or put in place a mechanism for shared transactions. Having to use Evict is (to me at least) a sign that you aren't managing session/transaction contexts well. I rarely ever use that method.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 13, 2007 8:39 pm 
Regular
Regular

Joined: Fri Jan 20, 2006 7:45 pm
Posts: 97
Location: San Antonio, TX
sorry for repeat


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