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.
When having to fetch multiple collections, you need to:
Hibernate.initialize
for the remaining collections.So, in your case, you need a first JPQL query like this one:
This way, you can achieve your goal with 2 SQL queries and avoid a Cartesian Product.
Use
Hibernate.initialize()
within@Transactional
to initialize lazy objects.Now out side of the Transaction you are able to get lazy objects.
You can traverse over the Getters of the Hibernate object in the same transaction to assure all lazy child objects are fetched eagerly with the following generic helper class: