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

后端 未结 2 1253
时光说笑
时光说笑 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 05:36

    The problem with lazy loading of OneToOne relationship is only on the inverse part of it (the one which is marked with mappedBy attribute). It works fine on the owning side of the relationship. T he difference between those is clear on database level. In your case the question is if the User database table holds an id of AvatarImage as one of the columns or the other way round. If User table has a column with an id of AvatarImage then the lazy loading will work as you said "out-of-box" but it will not work the other way round.

    0 讨论(0)
  • 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 <package_name>.AvatarImage_$$_javassist_0 (which is the proxy that allows for lazy fetching).

    0 讨论(0)
提交回复
热议问题