我所拥有的:
@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
}
真是个问题:
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.
什么样的解决方案(粗糙的解决方案) :
A)在关闭会话之前,强制休眠以拉取延迟集合
entity.getAddresses().size();
entity.getPersons().size();
....
B)也许更优雅的方法是使用 @Fetch(FetchMode.SUBSELECT)
注释
问题:
什么是最佳实践/常用方法/更优雅的方法? 意思是将我的对象转换为 JSON。