I got this javax.el.ELException org.hibernate.LazyInitializationException: failed to initialize proxy - no session

When I want to print the othre attribute attribute of the object of my education object on the XHTM page, I got an exception, with the exception of javax.el.ELException: Error reading 'intitule' on type model.Theme , this is my code:

 <h:form id="a"> <p:growl id="growl" showDetail="true" sticky="false" /> <p:dataTable var="formationObject" value="#{formationBean.listformation}" id="AjoutTab" widgetVar="FormationTable" emptyMessage="Formation non trouvé" rows="15" style="width:500px;font-size:13px;margin-left: 0px"> <f:facet name="header"> <p:outputPanel> <h:outputText value="Recherche:" /> <p:inputText id="globalFilter" onkeyup="FormationTable.filter()" style="width:80px ;float:center " /> </p:outputPanel> </f:facet> <p:column headerText="Intitule " id="formationRef" filterBy="#{formationObject.planning}" filterMatchMode="" footerText=" reférence exacte" width="15px"> <h:outputText value="#{formationObject.planning.intitule}" /> </p:column> <p:column style="width:4%"> <p:commandButton value="Analyser" icon="ui-icon-search" action="#{formationBean.redirectModification()}" ajax="false" title="Analyser" /> </p:column> </p:dataTable> </h:form> 
+4
source share
1 answer

LazyInitializationException is a very common mistake, and this almost always means that you either do not fully understand sleep mode proxies, or you do not control what is happening in your application.

When you use the @OneToMany annotation, hibernate usually uses lazy loading. This means that in the object, instead of the collection, you have a proxy , which does not contain an element, and loads the elements at the first request (for example, get() or size() ).

However, if the collection is accessed outside the Transactional , which in web applications usually means EL methods, the hibernate session associated with the proxy server no longer exists.

To prevent this behavior, you can choose two ways:

1) Do not use @OneToMany . Instead, if you want to get a collection, specify the DAO method to load this collection.

2) Make sure you never return an object with lazy proxies from the DAO method. You can iterate through a collection, set it to null, or pass a DTO through a mapper such as Dozer, which will call all getters and iterate over all collections, returning an object without a proxy. You can also call evict() in a hibernation session, but when iterating or setting it to null, you know whether the object was loaded or not.

+1
source

Source: https://habr.com/ru/post/1485534/


All Articles