-->
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: Updating domain objects in service layer
PostPosted: Tue Dec 13, 2005 7:38 am 
Senior
Senior

Joined: Sat Sep 03, 2005 12:54 am
Posts: 139
Hi,

The architecture that I have adopted for my application is as follows:

Domain
ServiceLayer
UI

The service layer class contain methods to add and update certain types of domain entities and these are passed in as objects to the methods.

Some objects that I want to update contain collections of child objects and it is possible that only some or none of the children have *actually* been changed. However, I still pass in the parent object into the Update method and then call the Session.Update on the parent and children as I am not sure which objects were changed in the UI, e.g.

public void Update(Order order)
{
...
this._session.Save(order);
foreach (OrderLine line in order.Lines)
{
this._session.Save(line);
}
...
}


In my tests, it seems that NH is smart enough to know that if I call Update on the session for an object that hasn't really changed since being loaded then it won't actually generate SQL against the DB...nice!! Saves me having to worry about tracking an objects dirty status.

Is this an OK design or is there something that I am missing here? Is there a better way to be coding my service layer?

Thanks,

Jason


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 13, 2005 9:33 am 
Contributor
Contributor

Joined: Thu May 12, 2005 9:45 am
Posts: 593
Location: nhibernate.org
Two suggestions:
- Use session.SaveOrUpdate()
- Enable cascading on your Lines collection so that the session automatically persist its items.

_________________
Pierre Henri Kuaté.
Get NHibernate in Action Now!


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 13, 2005 4:55 pm 
Senior
Senior

Joined: Sat Sep 03, 2005 12:54 am
Posts: 139
Oooops...the code was meant to call session.Update instead of session.Save! Is there any benefit in calling session.SaveOrUpdate as opposed to session.Update?

I added cascade="all-delete-orphan" to my collection to enable the automatic persistence of the collection but then started getting an error when adding new Parent/Child obejcts:

SQL insert, update or delete failed (expected affected row count: 1, actual affected row count: 0)...

The log file shows that NH is trying to Update the children in the collection instead of Inserting them into the DB when they are new. I think this is due to the fact that the Child has a composite ID that is made up of 2 other entities. Looking at the docos, it seems that NH can't distinguish between new and existing entities when using composite ID's and you have to call Save or Update on the child objects first. Is that right? Is there any alternative that can avoid that extra call?

Thanks,

Jason


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 13, 2005 7:48 pm 
Beginner
Beginner

Joined: Mon Aug 15, 2005 11:09 pm
Posts: 23
You could try implementing Interceptor.isUnsaved(). I personally haven't tried it though.


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.