-->
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.  [ 6 posts ] 
Author Message
 Post subject: Initialize Hibernate Proxy
PostPosted: Tue Oct 26, 2010 5:17 pm 
Newbie

Joined: Tue May 26, 2009 5:10 pm
Posts: 4
Hi there,

I have a User class with subclasses Client and Admin. In code I am trying to fetch a user with a particular id and then perform some action depending on the class of the user. Using instanceof method does not work as the object can be a hibernate proxy. Setting lazy=false in User class mapping is not an option for me due to performance requirements.

Is there an easy way for me force hibernate to initialize the proxy before using instanceof method? I have tried using Hibernate.initialize method but it does not seem to do anything. The session is open throughout the lifetime of the request.

Any help will be greatly appreciated.

Regards,
-jas


Top
 Profile  
 
 Post subject: Re: Initialize Hibernate Proxy
PostPosted: Wed Oct 27, 2010 2:10 am 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
It is possible to delve into the Hibernate-generated proxy to the get the underlying instance. Something like this:

Code:
if (object instanceof org.hibernate.proxy.HibernateProxy)
{
    HibernateProxy proxy = (HibernateProxy)object;
    Object actual = proxy.getHibernateLazyInitializer().getImplementation();
}


Top
 Profile  
 
 Post subject: Re: Initialize Hibernate Proxy
PostPosted: Wed Oct 27, 2010 1:16 pm 
Newbie

Joined: Tue May 26, 2009 5:10 pm
Posts: 4
Thank you. That works wonderfully.

-jas


Top
 Profile  
 
 Post subject: Re: Initialize Hibernate Proxy
PostPosted: Tue Feb 22, 2011 10:31 am 
Newbie

Joined: Tue Feb 22, 2011 10:26 am
Posts: 1
I'm having this issue as well and I would be grateful for some indication as to why the initialize method does not return the underlying object, instead of the proxy. I'm clearing my session and this is all being done within a transaction. There's no cache in my application.


Top
 Profile  
 
 Post subject: Re: Initialize Hibernate Proxy
PostPosted: Tue Feb 22, 2011 1:25 pm 
Newbie

Joined: Tue May 26, 2009 5:10 pm
Posts: 4
I am not sure why the initialize method doesn't work. Perhaps someone else can shed some more light on it.
But as suggested by nordborg, I wrote a utility method to initialize objects:

import org.hibernate.proxy.HibernateProxy;
...
public static Object initializeProxy(Object object) {
if (object instanceof HibernateProxy) {
HibernateProxy proxy = (HibernateProxy) object;
object = proxy.getHibernateLazyInitializer().getImplementation();
return object;
}
return object;
}
...


Top
 Profile  
 
 Post subject: Re: Initialize Hibernate Proxy
PostPosted: Tue Feb 22, 2011 2:45 pm 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
I am quite sure that the initialize method works (eg. it initializes the proxy or collection). The reason that it doesn't return the actual underlying instance is probably that the Hibernate developers don't want to break object identity within a session. At least that's my guess. On the other hand, object identity can be broken in other ways as described in http://docs.jboss.org/hibernate/core/3. ... ng-proxies Unfortunately that "workaround" can't be used to determine the actual subclass.


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