-->
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.  [ 12 posts ] 
Author Message
 Post subject: When to use Save/Update vs. automatic persistance?
PostPosted: Wed May 02, 2007 8:54 pm 
Newbie

Joined: Tue Feb 06, 2007 5:46 am
Posts: 10
I have been using NHibernate 1.2 now for several months and am able to do most things I try to do, but one thing that is still unclear to me is knowing when I need to manually call SaveOrUpdate or one of the other persistance methods versus when I can just let NHibernate manage this. What I've been dog is just trial-and-error--if something is not getting persisted, I add in a manual call, but I know this is not the best approach. So, I was wondering if someone could point me to the chapter in the docs that covers this and/or some other resource. I've looked around, but without much success. TIA

_________________
Josh Coady
http://jlcoady.net/


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 02, 2007 10:03 pm 
Regular
Regular

Joined: Sun Jan 21, 2007 4:33 pm
Posts: 65
Nhibernate can handle this automatically without a session.Save() call?

Holy cow, what else am I missing.


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 02, 2007 10:22 pm 
Newbie

Joined: Tue Feb 06, 2007 5:46 am
Posts: 10
Gortok wrote:
Nhibernate can handle this automatically without a session.Save() call?

Holy cow, what else am I missing.


Well, not always. I'm looking for someone to point me to a good resource on when it can and when it cant.

_________________
Josh Coady
http://jlcoady.net/


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 03, 2007 3:05 am 
Regular
Regular

Joined: Tue Aug 08, 2006 4:28 am
Posts: 96
Location: Hong Kong
AFAIK, NHibernate session will not automatically call Save() against an object unless you explicitly call Save(obj).

However, if you load an object through a session by whatever means, the session will automatically Update() the object when the session is flushed.

One feature in NHibernate is cascading save operation through object association. If two objects (e.g. objA and objB) are associated, You can just call session.Save(objA) and objB will automatically be saved. Please check documentation on cascade feature for details.

Hope this helps.


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 03, 2007 8:44 am 
Expert
Expert

Joined: Tue Aug 23, 2005 5:52 am
Posts: 335
Basically, an object has multiple states regarding persistence:

Transient - object has never been saved and does not exist in the database.

Persistent - object has been saved at some point and exists in the database and is currently associated with a Session.

Disconnected - object has been saved at some point but is not connected to a Session.

The thing to remember is that to move an object from Transient to Persistent you can use Save or SaveOrUpdate. Once an object has been made persistent it can be reloaded using Load, Get or a query.

The state of any persistent object will be updated in the database automatically while it remains persistent (ie. while it's connected to a session). As soon as it's not persistent it's state is not automatically transferred to the database.

A persistent object can stop being persistent in one of two ways; either it can be deleted using Delete which will cause the data in the database to be deleted and the object will become transient again, or the session can be closed and the object can remain in memory as a disconnected object.

Disconnected objects can be re-connected to a session (and therefore become persistent again) using Update or SaveOrUpdate. SaveOrUpdate is actually a simple shortcut that prevents you from having to know whether an object is transient or disconnected and will just make it persistent, but it decides which one it is based on the "unsaved-value" of the object's Id.

Don't know if that really helps, but hopefully it clarifies things a little.

Cheers,

Symon.


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 03, 2007 11:19 am 
Newbie

Joined: Tue Feb 06, 2007 5:46 am
Posts: 10
So, transient objects need an explicit call to Save or SaveOrUpdate to be persisted, but as long as there is an open session persistant objects will be automatically updated?

What if you add a transient object to a collection property of a persistant object?

Thanks!

_________________
Josh Coady
http://jlcoady.net/


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 03, 2007 11:37 am 
Expert
Expert

Joined: Tue Aug 23, 2005 5:52 am
Posts: 335
As far as I know, if you enable cascading for a collection mapping it will handle making those child objects persistent for you automatically.

Symon.


Top
 Profile  
 
 Post subject:
PostPosted: Sun May 06, 2007 12:48 am 
Senior
Senior

Joined: Mon Aug 21, 2006 9:18 am
Posts: 179
One thing to keep in mind about NHibernate semantics is this: 'Save' or 'SaveOrUpdate' do not really 'save' the object's values to the Database. The calls to these methods simply add the object to the Unit Of Work to be persisted when session.Flush() is called or the Transaction is .Commit()-ed.
The transparent write-behind (the behaviour where NHib 'automatically' saves the objects for you) assumes you are using the FlushMode.Auto so that when the Session is closed the Flush method occurs and all those obejcts you 'saved' are then persisted to the db.
I don't use this method but it is very common, especially with web apps and examples you find on the net.

Mike

_________________
If this helped...please remember to rate it!


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 07, 2007 2:56 am 
Beginner
Beginner

Joined: Sun Oct 22, 2006 12:06 pm
Posts: 39
I've managed this authomatic persistent by properties.
Every Entity object extends BusinesObject class.

Code:
public class User:BusinessObject<User>
{

public String name
{
get{ return _name;}
set {
         _name=value;
        base.PropertyHasChanged("Name");
}

}

}


And in BusinessObject PropertyHasChanged will invoke
session.SaveOrUpdate(this);

Also I have made some extra control functoins like SuspendSave(boolean) and things like that, when I need to do something more complicated


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 07, 2007 10:25 am 
Senior
Senior

Joined: Thu Feb 09, 2006 1:30 pm
Posts: 172
pyhavaim wrote:
Code:
public class User:BusinessObject<User>
{

public String name
{
get{ return _name;}
set {
         _name=value;
        base.PropertyHasChanged("Name");
}

}

}


And in BusinessObject PropertyHasChanged will invoke
session.SaveOrUpdate(this);

Also I have made some extra control functoins like SuspendSave(boolean) and things like that, when I need to do something more complicated


pyhavaim,

This code seems a little odd in the NHibernate context. NHibernate does the work you just described automatically. So it looks like you created your own framework to duplicate part of the NHibernate framework.

Keep in mind what a previous poster said. Save/Update/SaveOrUpdate are not methods which are used to save/update to a database. It's used to add the objects to the session (unit of work). That doesn't mean that a database call won't be made at that point (it may) but we really don't control when those calls are made.

Whenever an object is loaded into an NHibernated session it will create an internal object to store the state of the object when it was loaded so that when flush is called it can compare the current values and compare them to that of the initially loaded values. If a value has changed it will save that change to the database.

Writing a framework which does the same thing seems counterproductive.


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 07, 2007 11:00 am 
Beginner
Beginner

Joined: Sun Oct 22, 2006 12:06 pm
Posts: 39
Well, thanx!

I'll try this. I was kind a happy that I did'nt had to worry about when to save objects.

It turned out that I did'nt had to worry, there is something wrong in configuration that changes did'nt persist.


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 07, 2007 9:34 pm 
Regular
Regular

Joined: Tue Aug 08, 2006 4:28 am
Posts: 96
Location: Hong Kong
pyhavaim, care to share what's the problem in configuration? Perhaps others have the same problems as you do.


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