How does Hibernate create proxies of concrete clas

2019-02-04 15:47发布

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?

4条回答
叼着烟拽天下
2楼-- · 2019-02-04 16:19

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

查看更多
太酷不给撩
3楼-- · 2019-02-04 16:20

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

hibernate.bytecode.provider=javassist
查看更多
Fickle 薄情
4楼-- · 2019-02-04 16:27
太酷不给撩
5楼-- · 2019-02-04 16:38

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

Hibernate Core Migration Guide : 3.3

查看更多
登录 后发表回答