I have an entity Parent and a relation @OneToMany
to another entity Child. The collection children is set to lazy loading.
@Entity
class Parent{
@Id
String parentId;
@OneToMany(mappedBy="parent",fetch=FetchType.LAZY)
List<Child> children;
}
@Entity
class Child{
@Id
String childId;
@ManyToOne
@JoinColumn(name="parentId")
Parent parent;
}
List<Parent> loadParents() {
QParent qParent = QParent.parent;
List<Parent> parentList = query.select(qParent).from(qParent).fetch();
return parentList;
}
@Transactional
void test(){
List<Parent> parentList = loadParents();
for(Child child:parentList.getChildren()){
child.getChildId();
}
}
I get the famous
org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role ... could not initialize proxy - no Session
exception in the test() method on the line where I access the children list.
I don't want to change the fetch type annotation in the entity.
How do I access the child entities?