To the best of my knowledge, creating a dynamic Java proxy requires that one have an interface to work against for the proxy. Yet, Hibernate seems to manage its dynamic proxy generation without requiring that one write interfaces for entity classes. How does it do this? The only clue from the Hibernate documentation refers to the fact that classes must have at minimum a package-visible constructor for proxy generation.
Is Hibernate doing runtime bytecode engineering with a custom classloader? The documentation suggests that this is not the case. So how do they create their proxy wrappers around the concrete entity objects? Do they just create a proxy of some trivial interface without concern for type safety and then cast it as desired?
To use the proxy creator that is in java, yes, you are correct, but I believe hibernate uses CGLib to generate its proxies.
http://cglib.sourceforge.net/
Its a cool program, and doesn't require interfaces to generate a proxy.
(and maybe someday hibernate will move to using AOP and the world will be a better place).
Hibernate uses the bytecode provider configured in
hibernate.properties
, for example:See class javassist.util.proxy.ProxyFactory for details.
Since Hibernate 3.3, the default bytecode provider is now Javassist rather than CGLib.
Hibernate Core Migration Guide : 3.3