-->
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: Determine whether an object is detached or not
PostPosted: Wed Dec 05, 2007 11:51 am 
Newbie

Joined: Wed Dec 05, 2007 11:23 am
Posts: 3
Hello everyone!
I'm currently developing a web application (using one session per view), roughly separated in 3 tiers: data access, business and UI. The process tier is session agnostic.

The problem is, I intended to keep a reference to the data objects (fetched by NHibernate) at UI level, in order to reuse them in the following sessions (for instance, when logging in a user I think I could fetch the user info only the first time and then keep it, or when reading an article I could fetch the article's data and the reuse it in case the user wants to comment or tag it). Is this approach fundamentally flawed?

But anyway, the real question is that when I'm reusing some detached object in the business tier I need a way to reattach it to the current session. Currently I simply call SaveOrUpdate on each object the business tier reuses, but I fear this might be inefficient when calling it multiple times on objects that are already attached (because they were fetched in the current session: but the business layer has no way to know it).
I'm looking for a way to find out if an object is attached to a particular session or not: I tried to use NHibernateProxyHelper in order to get the objects lazy initializer and then compare the sessions, but this doesn't work (it seems the objects I fetch through NHibernate are not proxied). Isn't there a "IsAttached(object o)"-like method somewhere?

Thank you very much. :)


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 05, 2007 12:38 pm 
Expert
Expert

Joined: Fri Oct 28, 2005 5:38 pm
Posts: 390
Location: Cedarburg, WI
To find out if an entity is already attached to a particular session, use
Code:
session.Contains(entity)


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 05, 2007 1:12 pm 
Expert
Expert

Joined: Thu Jul 05, 2007 9:38 am
Posts: 287
In order to attach an object to a session I'd recommend to look at
- refresh (load the current state from the database in the current object)
- get / load (loads the current state from session or if necessary from the database)

- using update or merge is possible but probably not what you want, because it causes updates and/or exceptions, when the object is changed in the meantime

In order to prevent excessive database roundtrips configure a 2nd level cache with short timeout times (1minute!?) so that repeatet accesses to the same object via get or load get satisfied by the cache

HTH
Jens


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 05, 2007 2:42 pm 
Newbie

Joined: Wed Dec 05, 2007 11:23 am
Posts: 3
Thanks a lot, exactly what I was looking for. Works like a charm! :)

schauder wrote:
In order to attach an object to a session I'd recommend to look at
- refresh (load the current state from the database in the current object)
- get / load (loads the current state from session or if necessary from the database)


Yes, I will definitely give a look to 2nd level caching. Do you know if the Refresh call always results in a database roundtrip or will it eventually check the cache too?
Thank you.


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.