JPA 2.0 / Hibernate: Why does LAZY fetching with “@OneToOne” work out of the box?

后端 未结 2 1254
时光说笑
时光说笑 2021-01-18 05:10

my question is regarding JPA 2.0 with Hibernate, @OneToOne relationships and lazy loading.

First my setup:

  • Spring 3.0.5.RELEASE
  • SprnigData JPA
2条回答
  •  旧巷少年郎
    2021-01-18 06:02

    Lazy fetching works out of the box for @OneToOne annotated relationships, with the Hibernate JPA provider, when some form of bytecode instrumentation is performed. In your case, we could rule out build-time instrumentation (inferring from your comment that it works out-of-the-box). This leaves you with the possibility of runtime weaving, which is quite possible in Hibernate & Spring. In recent releases of Hibernate, Javassist is used as the runtime bytecode instrumentation framework for Hibernate, as opposed to the other alternative of CGLIB (which has been deprecated since Hibernate 3.5.5).

    The question of whether Javassist is enabled in Spring is quite simple to answer. Hibernate EntityManager (which is the JPA 2.0 provider that delegates to Hibernate Core), requires Javassist, and therefore, it ought to be in the classpath of Hibernate, allowing for runtime weaving of the classes. You can confirm this by setting a breakpoint (in a remote debugger connected to your application server), and you'll notice that a Hibernate managed instance of the User class will not contain a reference to an AvatarImage instance; rather it would contain a reference to an enhanced class with a name like .AvatarImage_$$_javassist_0 (which is the proxy that allows for lazy fetching).

提交回复
热议问题