-->
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.  [ 1 post ] 
Author Message
 Post subject: Lazy loading target class type
PostPosted: Fri Jun 18, 2010 8:40 am 
Newbie

Joined: Mon May 24, 2010 9:12 am
Posts: 3
Hello,

I have a class hierarchy mapped with single table strategy: "Person" and "Student" which extends Person.

I lazyload a Person entity with the following:

Code:
   Person p = HibernateUtil.getCurrentSession().load(Person.class, 1);


From previous code I know that Person with id 1 is indeed a Student.
If I use instanceof java operator to test its type it does not work because p is actually of type Person_$$_javassist_32 (and Student does not extend it).

So I write this method to test if the entity is of type HibernateProxy and to test the target implementation the proxy implements:

Code:
   public static boolean instanceOf(Object obj, Class<?> clazz) {
      if (obj instanceof HibernateProxy)
         return clazz.isInstance(((HibernateProxy)obj).getHibernateLazyInitializer().getImplementation());
      else
         return clazz.isInstance(obj);
   }


With this method everything works fine. I can call the followings getting the results i expected:

Code:
   instanceOf(p, Person.class); //true
   instanceOf(p, Student.class); //true



The bad thing is that instanceOf() method needs the session to be active, because ((HibernateProxy)obj).getHibernateLazyInitializer().getImplementation() does initialize the proxy to get the real target class type.

In substitution I tried to use the following:

Code:
   clazz.isAssignableFrom(HibernateProxyHelper.getClassWithoutInitializingProxy(obj));


but it does not work, because HibernateProxyHelper.getClassWithoutInitializingProxy(obj) returns Person.class, even if the target class type is Student.class (I can see it use Eclipse debugger...)

Is there another solution to get the real class type without initialize the proxy?
Could not be seen like a bug that HibernateProxyHelper.getClassWithoutInitializingProxy() returns the proxy superclass type instead of the target class type? (or maybe is there another method to get the target class type from a superclass proxy??).

Thanks in advance
Luca


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

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.