What I have:
@Entity
public class MyEntity {
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, orphanRemoval = true)
@JoinColumn(name = "myentiy_id")
private List<Address> addreses;
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, orphanRemoval = true)
@JoinColumn(name = "myentiy_id")
private List<Person> persons;
//....
}
public void handle() {
Session session = createNewSession();
MyEntity entity = (MyEntity) session.get(MyEntity.class, entityId);
proceed(session); // FLUSH, COMMIT, CLOSE session!
Utils.objectToJson(entity); //TROUBLES, because it can't convert to json lazy collections
}
What a problem:
The problem is that I can't pull lazy collection after session has been closed. But I also can't not close a session in proceed method.
What a solution (coarse solution):
a) Before session is closed, force hibernate to pull lazy collections
entity.getAddresses().size();
entity.getPersons().size();
....
b) Maybe more ellegant way is to use @Fetch(FetchMode.SUBSELECT)
annotation
Question:
What is a best practice/common way/more ellegant way to do it? Means convert my object to JSON.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…