views:

1206

answers:

4

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?

+3  A: 

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).

runT1ME
yes, it does use cglib
Vladimir Dyuzhev
+5  A: 

Since Hibernate 3.3, the default bytecode provider is now Javassist rather than CGLib.

http://www.hibernate.org/250.html#A56

Andrew Newdigate
+2  A: 

See class javassist.util.proxy.ProxyFactory for details.

Martin Burger
+2  A: 

Hibernate uses the bytecode provider configured in hibernate.properties, for example:

hibernate.bytecode.provider=javassist
Maciek Kreft