The attached test case contains two entity classes, connected by a @OneToOne relation:
@Entity
public class Parent {
@Id
private Long id;
@OneToOne(mappedBy = "parent", optional = false, fetch = FetchType.LAZY)
private ChildWithFk child;
...
}
@Entity
public class ChildWithFk {
@Id
private Long id;
@OneToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "PARENT_ID", nullable=false)
private Parent parent;
...
}
The internet is full of smart advice that you could tell Hibernate to lazy-load the OneToOne relation even on the non-owner side, if (and only if) the relation is optional=false.
The given explanation is usually: a lazy-loading proxy can be inserted on that side, as we know that the relation can't be null (due to the optional=false).
I was always under the impression that it actually works, but I can't get it to work in my application now and also not in the attached example. I've tried different Hibernate versions, but I always get the same result (run mvn clean test -Dhibernate.version=... to try out a few on your own, if that helps): the relation Parent.child is always fetched eagerly with a second query.
Not sure if this is actually a bug, or a feature request. :wink:
|