问题
I have Entity like that
@Entity
@Table(...)
public class ENTITY_1 {
//....
private ENTITY_2 entity_2 ;
private ENTITY_3 entity_3 ;
private Set<ENTITY_3> entities_3 = new HashSet<ENTITY_3>(0); ;
private ENTITY_4 entities_4 = new HashSet<ENTITY_4>(0); ;
//...
and i want to display a result of a query in my jsp so i make a request with fetch to get all the related entities to avoid the lazy initialization exception left join fetch
.
My request is based on my ENTITY_1 (select .. from ENTITY_1
)
But i use pagination just 10 result per page so the fetch slow down my request So i used @BatchSize(size=10)
.
My problem is how to display all the data of related entities in my jsp because i got the lazy initialization exception.
My jsp be like :
${entity_1_model.discription}
//...
<c:forEach var="entity_4" items="${entity_1_model.entities_4}">
<span class="">${entity_4.name}</span>
</c:forEach>
回答1:
The quickest and easiest solution would be to fetch the data after you execute the main query. Try something like this
page.content = query.offset(pageNumber*pageSize).limit(pageSize).list(ao);
for (Content cont : page.content) {
Hibernate.initialize(cont.getEntities_4());
}
来源:https://stackoverflow.com/questions/27105824/display-data-from-related-entities-to-avoid-the-lazy-initialization-exception-wi