-->
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: can session be injected in persistent object ?
PostPosted: Mon Apr 25, 2005 2:31 pm 
JBoss jBPM Developer
JBoss jBPM Developer

Joined: Thu Aug 28, 2003 11:56 am
Posts: 30
Location: Belgium
Hibernate version: 3.0 final

Is there a way that hibernate could inject the session into a persistent object ?

I had a quick look but couldn't find a solution with the lifecycle and callback methods. Maybe i overlooked.

regards, tom.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 25, 2005 4:59 pm 
Regular
Regular

Joined: Mon Jul 26, 2004 2:28 pm
Posts: 86
Location: Pensacola, Florida
The way I understand Hibernate, the object really doesn't have a clue that it's being managed, meaning it doesn't really have a handle to its associated session (if there is one). The obvious exception is with proxies and lazy loading...the proxy object has to have a reference to the session in order to perform the lazy load. If by "inject" a session into a persistent object you mean attaching the persistent object to a session, this can be accomplished by calling session.lock( object, LockMode.NONE ) or session.update( object ). This will associate a proxy with the (new?) session and allow lazy loading to occur.

I imagine that you could add a session property to your POJOs and write an interceptor that would set the session property during lifecycle events so that the object has a handle to the session. I don't think that such an approach is recommended, however, as there is a potential to have a handle to the wrong session stored somewhere in one of your POJOs. It might also introduce some interesting side-effects if you are using proxies if you are using those.

The ThreadLocal pattern (HibernateUtil) with a request interceptor (javax.servlet.Filter) has worked well for me and I think is considered a best practice. Rather than associating the session with an object it is associated with the Thread, so your scoping concerns are mitigated.

- Jesse


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 26, 2005 12:20 pm 
JBoss jBPM Developer
JBoss jBPM Developer

Joined: Thu Aug 28, 2003 11:56 am
Posts: 30
Location: Belgium
i know it's not recommended, but i can't use the interceptor method

boolean onLoad(Object entity, Serializable id, Object[] state, String[] propertyNames, Type[] types)

since it doesn't give me a pointer to the session.

regards, tom.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 26, 2005 12:23 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 3:00 pm
Posts: 1816
Location: Austin, TX
This is *really* not recommended, but you could use the Lifecycle interface to acheive this. In general though, like Jesse mentioned, it is not a good idea for your domain entities to have any knowledge of a Hibernate Session (or Hibernate-specific classes at all for that matter).


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.