A regression was introduced when load plans were introduced in 4.3 that results in many-to-one associations to be eagerly loaded if they are embedded in an embeddable collection element. For example:
@Entity(name = "Parent")
public static class Parent {
@Id
@GeneratedValue
private int id;
private ContainedChild containedChild;
@ElementCollection
private Set<ContainedChild> containedChildren = new HashSet<ContainedChild>();
}
@Entity(name = "Child")
public static class Child {
@Id
@GeneratedValue
private int id;
}
@Embeddable
public static class ContainedChild {
@ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
private Child child;
ContainedChild() {
}
ContainedChild(Child child) {
this.child = child;
}
}
When a Parent entity is loaded, containedChild.child will be lazy (as expected). When a Parent#containedChildren is initialized, the child contained in each ContainedChild collection element will be non-lazy. These child entities should be lazy. I believe that many-to-one associations in composite IDs will also be eagerly loaded, even when mapped as lazy. I need to look into this further though. |