I use a 'deproxy()' and a couple of other helper functions, for (as you say) certain kinds of code.
This makes Spring 'autogrow' path binding work, it's necessary for checking actual types when loading possibly subclassed entities thru Hibernate, and for various other amusing corner cases where proxying causes things to break.
public class HbUtils {
public static <T> T deproxy (T obj) {
if (obj == null)
return obj;
if (obj instanceof HibernateProxy) {
// Unwrap Proxy;
// -- loading, if necessary.
HibernateProxy proxy = (HibernateProxy) obj;
LazyInitializer li = proxy.getHibernateLazyInitializer();
return (T) li.getImplementation();
}
return obj;
}
public static boolean isProxy (Object obj) {
if (obj instanceof HibernateProxy)
return true;
return false;
}
// ----------------------------------------------------------------------------------
public static boolean isEqual (Object o1, Object o2) {
if (o1 == o2)
return true;
if (o1 == null || o2 == null)
return false;
Object d1 = deproxy(o1);
Object d2 = deproxy(o2);
if (d1 == d2 || d1.equals(d2))
return true;
return false;
}
public static boolean notEqual (Object o1, Object o2) {
return ! isEqual( o1, o2);
}
// ----------------------------------------------------------------------------------
public static boolean isSame (Object o1, Object o2) {
if (o1 == o2)
return true;
if (o1 == null || o2 == null)
return false;
Object d1 = deproxy(o1);
Object d2 = deproxy(o2);
if (d1 == d2)
return true;
return false;
}
public static boolean notSame (Object o1, Object o2) {
return ! isSame( o1, o2);
}
// ----------------------------------------------------------------------------------
public static Class getClassWithoutInitializingProxy (Object obj) {
if (obj instanceof HibernateProxy) {
HibernateProxy proxy = (HibernateProxy) obj;
LazyInitializer li = proxy.getHibernateLazyInitializer();
return li.getPersistentClass();
}
// Not a Proxy.
return obj.getClass();
}
}
I use a class like this in every major Hibernate project. PS: This isn't my only Hibernate helper -- I have one for generically getting IDs and checking if entities are New/Existent.
Hope this helps!