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