Display data from related entities to avoid the lazy initialization exception with setting up @BatchSize

不问归期 提交于 2020-01-17 08:11:13

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!