Volodymyr Bakhmatiuk Volodymyr Bakhmatiuk - 5 months ago 82
Java Question

Hibernate: best practice to pull all lazy collections

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.

Answer

Use Hibernate.initialize() within Transactional to initialize lazy objects.

 start Transaction 
      Hibernate.initialize(entity.getAddresses());
      Hibernate.initialize(entity.getPersons());
 end Transaction 

Now out side of the Transaction you are able to get lazy objects.

entity.getAddresses().size();
entity.getPersons().size();
Comments