I'm using Hibernate with proxies, and I get objects belonging to classes such as test.DBUser$$EnhancerByCGLIB$$40e99a2d
.
Is there a Hibernate method to retrieve the base class (test.DBUser
in this case) from the proxied class? I know about Hibernate.getClass()
, but it takes an Object
, while I'm looking for a method which takes as input a Class
.
While I really like the simplicity of the approach posted by Flavio, I can't use it in production code unless it's documented as supported. Also, if you call .getImplementation() on the LazyInitializer, it will force the initialization of the proxy if it isn't already, which is a negative performance impact. I've come up with this approach which addresses both of these concerns:
There is no such method. You will need to write a helper class yourself in order to retrieve the wrapped object and thus the class information from inside the proxy. If you only need the object in the given scenario, try to remove all the lazy loading. Hibernate should then give you the plain object.
Consider trying not to need the object. Maybe you can redesign the application so that you do not need it, for example by adding a field containing the desired information at runtime.
Classes like
test.DBUser$$EnhancerByCGLIB$$40e99a2d
are dynamic proxies. The concept of "real class behind" does not make much sense in most cases. Every single time a proxy is created, it can be instance of any class as Hibernate defines it.What you are really asking for is static
Map
of{ Class<Proxy>, Class<RealObject>}
. I don't believe there's such a thing nor I believe there's a need for this. Just look at the source ofHibernate.getClass()
:It would be much much cheaper to do a static map lookup to get the real class, but Hibernate goes all the way to the lazy initializer to get the implementing class.
I found out, it is easier than I thought: just call
getSuperclass()
on the proxied class to obtain the unproxied, original class. I'm not sure how general this is, but it appears to work.