I've seen several similar questions with 0 good answers.
This should be very simple. I'm using Java JPA and I want to load a List of child entities sometimes but not all the time. Unfortunately, JPA doesn't seem to be listening to me when I say to fetch it lazily. I've made 100% certain that there's nowhere in my code where the childEntities attribute is being accessed in any way. However, all the child entities are still getting loaded immediately upon my JPA.em().find(..) call. This is how I declare the relationship with annotations.
@Entity
@Table(name = "parentEntities")
public class ParentEntity implements Serializable {
....
@OneToMany(mappedBy = "entityPropertyName", fetch = FetchType.LAZY)
public List<ChildEntity> childEntities;
...
}
And this is how I load the parent entity:
ParentEntity parentEntity = JPA.em().find(ParentEntity.class, id);
Additionally, I was hoping to sometimes eager fetch this collection and be able to dynamically tell JPA when to do so. That's step 2 though. Step 1 is just to get this working right.