-->
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: Cascade SaveOrUpdate
PostPosted: Tue Dec 22, 2009 8:43 am 
Newbie

Joined: Tue Dec 22, 2009 8:38 am
Posts: 18
Hello everyone,

I am interested whether NHibernate supports a cascade SaveOrUpdate by default.
Basically I have 2 entities: Client and ClientType. When I save this association, if the ClientType.Name value already exists in the database, I only want to update it.

Is this feature available by default in NHibernate?

Thank you,
C.


Top
 Profile  
 
 Post subject: Re: Cascade SaveOrUpdate
PostPosted: Tue Dec 22, 2009 9:44 am 
Newbie

Joined: Tue Mar 07, 2006 4:56 am
Posts: 17
Hi Catalinus,

I guess the answer is yes and no. SaveOrUpdate is only useful if you work with an decoupled instance of your entity. Meaning, if you have an object in memory, which is not connected to a hibernate session, hibernate does not yet know what to do with it when you "add" it to the session. SaveOrUpdate will be the most resource consuming (I would assume), as it will try to figure out by itself if this entity already exists in the database or not, while a pure save will assume that it is a new instance.

So, the answer here is no. You have to do a Save, Update or SaveOrUpdate on an object, that is not connected to a session.

If your object is loaded from a session and is still connected to it and you have configured your hbm file that way, it will cascade the changes upon a Commit (in case of a transaction) or Flush on the session. I'm not quite certain if this is the default behaviour or not, since we did some changes in code to emulate the 1.x default behaviour in our application when we migrated to 2.x.

I hope that my answer will help you a little.

Kind regards, Marco...


Top
 Profile  
 
 Post subject: Re: Cascade SaveOrUpdate
PostPosted: Wed Dec 23, 2009 10:51 am 
Newbie

Joined: Tue Dec 22, 2009 8:38 am
Posts: 18
Hi Marco,

Thank you for your quick answer. I will detail a little bit the problem.

As I told you. I have 2 entities (Client and ClientType) mapped to their corresponding tables in the database.
I want to save a new client. This could mean:
- if the client already exists in the database (I could check this, depending on the values of several fields) it will simply update the changed fields, one of them being ClientType field. And on this case, we have 2 sub-cases:
- if the inserted ClientType is new, it should insert it in the ClientType table and update the Client table with the corresponding reference, or
- if the inserted ClientType already exists in the ClientType table, it will simply assign the corresponding reference to the Client table.
- if the client does not exist in the database, it will insert it, taking into consideration the previously mentiones issues with the ClientType field:
- if the inserted ClientType is new, it should insert it in the ClientType table and update the Client table with the corresponding reference, or
- if the inserted ClientType already exists in the ClientType table, it will simply assign the corresponding reference to the Client table.
(to be continued ...)


Top
 Profile  
 
 Post subject: Re: Cascade SaveOrUpdate
PostPosted: Wed Dec 23, 2009 11:04 am 
Newbie

Joined: Tue Dec 22, 2009 8:38 am
Posts: 18
(continuing ...)

I have tried to do this in NHibernate with SaveOrUpdate, but with no success:
- If I am doing SaveOrUpdate on a new Client assigned to a new ClientType, NHibernate inserts 1 new record in Client and 1 new record in ClientType.
- If I am doing SaveOrUpdate on a new Client assigned to an existing ClientType, NHibernate behaves the same ... duplicating the ClientType instead of using an already inserted one, to reference it to the table Client.

Maybe I am missing something, thats why I have asked you whether we can solve this case with SaveOrUpdate, and what would be the alternatives if not.

Thank you,
C.


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.