-->
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.  [ 5 posts ] 
Author Message
 Post subject: Update in a web application design question
PostPosted: Thu Aug 09, 2007 7:51 am 
Newbie

Joined: Thu Aug 09, 2007 7:24 am
Posts: 3
Hello,

I am just starting to use NHibernate, but I have a design question.

Take a standard web application where I would need to update an object

Http Request 1: get object you need to update
Http Request 2: update object, for this I see 2 ways:

1) Do a Load() of the object, update the values, save (but if I get it correctly then you'd do an unnecessary select on the database before updating)

2) Use the Update (or SaveOrUpdate method), no select on database, but if your object is by any chance in your session already it will throw an exception (as specified by the doc). I read in the documention and examples that it's usual to use a single session for an entire http request so there is a chance the object will already be in session.

I'm pretty sure number 2 is the way to go since every example I've looked at uses this almost, but how can I prevent the error I get ? In a lot of examples they don't seem to be ckecking for it and just assume it won't be in session already. What would be the best way to design this ?

Some extra info: I use a singleton for my sessionfactory to pass out sessions (as in the quickstart example). (going to change that to a HttpModule though, I got a nice example in the Benday Bugtracker example).

Thanks in advance,

Jorn


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 09, 2007 11:27 am 
Hibernate Team
Hibernate Team

Joined: Tue Jun 13, 2006 11:29 pm
Posts: 315
Location: Calgary, Alberta, Canada
With scenario #2, it is theoretically true that you could be updating an entity that is already in the session and cause an exception. In practice, especially if you are using an HttpModule to open and close a session with every request (a.k.a. Session-In-View pattern), you are unlikely to have that problem since you get a new session with every request. It may not be comforting enough to know that it is only unlikely to give you problem. In that case, you can use the SaveOrUpdateCopy() method.

_________________
Karl Chu


Top
 Profile  
 
 Post subject:
PostPosted: Fri Aug 10, 2007 3:07 am 
Newbie

Joined: Thu Aug 09, 2007 7:24 am
Posts: 3
Thank you for your reply, that will indeed fix my problem to not get an exception anymore.

But won't this in most cases also result in a select of the object to the database then, just like the Load() in method number 1) I stated ?

Thinking about it, won't this always be the case with NHibernate in a web application ? For every update you need to do in a web application, NHibernate will always have to select the object before updating it ? Is there a way to avoid this ?


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

Joined: Tue Jun 13, 2006 11:29 pm
Posts: 315
Location: Calgary, Alberta, Canada
Darthy wrote:
But won't this in most cases also result in a select of the object to the database then, just like the Load() in method number 1) I stated ?


I don't believe so. NHibernate only compares the type and id of the object. By virtue of calling SaveOrUpdateCopy(), you are in essence telling NHibernate to overwrite the entity object in the session with the entity object you just gave it.

Darthy wrote:
Thinking about it, won't this always be the case with NHibernate in a web application ? For every update you need to do in a web application, NHibernate will always have to select the object before updating it ? Is there a way to avoid this ?


Not if you keep a reference of the entity around (e.g. in the ASP.NET session) from request to request. In this case, you would be working with detached entities in the postbacks. Have a read of the "Updating Objects" section of the docs:
http://www.hibernate.org/hib_docs/nhibe ... a-updating

_________________
Karl Chu


Top
 Profile  
 
 Post subject:
PostPosted: Sun Aug 12, 2007 7:08 am 
Newbie

Joined: Thu Aug 09, 2007 7:24 am
Posts: 3
Ok thanks :)

I had read that part of the doc, and I guess I was confused by it stating

Quote:
This method copies the state of the given object onto the persistent object with the same identifier. If there is no persistent instance currently associated with the session, it will be loaded.


I then assumed it woud load it from the database (since sometimes my object will already exist, sometimes not)


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