Error could not initialize proxy - no Session

Hello,

I reatch an issue with my developpement.

I have an object myObject like this :

@Entity
@Inheritance(strategy=InheritanceType.TABLE_PER_CLASS)
public abstract class AbstractEntity {
 	@Id
	@Basic(optional = false)
	@GeneratedValue(strategy = GenerationType.TABLE)
	private int id;
}

and an extended class

@Entity
@Table(uniqueConstraints=@UniqueConstraint(columnNames={"EMAIL", "LICENCENUMBER"}))
public class Diver extends AbstractEntity {

	@NotNull
	@ElementCollection(targetClass = TechnicalRole.class, fetch = FetchType.EAGER )	
	@Enumerated(EnumType.STRING)
	private Set<TechnicalRole> roles;
 ....
} 

I interact with this object throught a web page using JSF 2 (RicheFace) deployed in an WAR, a JAR containing my persistance model, an EJB module for the DAO. Every thing is packaged in a ear deployed on the last WILDFLY 25.0.1 .

I configure my persistence.xml like this :

    <persistence-unit name="myName" transaction-type="JTA" >
     	
     	<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
        	<jta-data-source>java:jboss/datasources/ExampleDS</jta-data-source>
        	<jar-file>myJar</jar-file>
        	<class>Diver</class>
        	<class>other Class</class>
     	<properties>
              <property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect"/>
              <property name="hibernate.show_sql" value="false" />
              <property name="hibernate.hbm2ddl.auto" value="create"/>
       	
              <property name="hibernate.jpa.compliance.transaction" value="true"/>
              <property name="hibernate.jpa.compliance.closed" value="true"/>
              <property name="hibernate.jpa.compliance.proxy" value="true"/>
 	       	
              <property name="jboss.entity.manager.factory.jndi.name" value="java:jboss/DivingReservationEntityManagerFactory" />
              <property name="jboss.entity.manager.jndi.name" value="java:/DivingReservationEntityManager"/>
 			
              <property name="javax.persistence.validation.mode" value="ddl, callback" />
          </properties>
     </persistence-unit>

During the RENDER_PHASE of the JSF, I get the following error :

 12:19:00,938 SEVERE [javax.enterprise.resource.webcontainer.jsf.context] (default task-1) org.hibernate.LazyInitializationException: failed to lazily initialize a collection, could not initialize proxy - no Session
 	at org.hibernate@5.3.23.Final//org.hibernate.collection.internal.AbstractPersistentCollection.throwLazyInitializationException(AbstractPersistentCollection.java:602)
 	at org.hibernate@5.3.23.Final//org.hibernate.collection.internal.AbstractPersistentCollection.withTemporarySessionIfNeeded(AbstractPersistentCollection.java:217)
 	at org.hibernate@5.3.23.Final//org.hibernate.collection.internal.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:581)
 	at org.hibernate@5.3.23.Final//org.hibernate.collection.internal.PersistentSet.add(PersistentSet.java:210)
 	at com.sun.jsf-impl@2.3.14.SP04//com.sun.faces.renderkit.html_basic.MenuRenderer.convertSelectManyValuesForCollection(MenuRenderer.java:369)
 	at com.sun.jsf-impl@2.3.14.SP04//com.sun.faces.renderkit.html_basic.MenuRenderer.convertSelectManyValuesForModel(MenuRenderer.java:228)
 	at com.sun.jsf-impl@2.3.14.SP04//com.sun.faces.renderkit.html_basic.MenuRenderer.convertSelectManyValue(MenuRenderer.java:109)
 	at com.sun.jsf-impl@2.3.14.SP04//com.sun.faces.renderkit.html_basic.MenuRenderer.getConvertedValue(MenuRenderer.java:206)
 	at deployment.DivingReservationEAR.ear.DivingReservationWebPrimeFace.war//org.primefaces.component.selectmanybutton.SelectManyButtonRenderer.getConvertedValue(SelectManyButtonRenderer.java:53)
 	at javax.faces.api@3.0.0.SP04//javax.faces.component.UIInput.getConvertedValue(UIInput.java:1110)
 	at javax.faces.api@3.0.0.SP04//javax.faces.component.UIInput.validate(UIInput.java:1011)
 	at javax.faces.api@3.0.0.SP04//javax.faces.component.UIInput.executeValidate(UIInput.java:1322)
 	at javax.faces.api@3.0.0.SP04//javax.faces.component.UIInput.processValidators(UIInput.java:733)
 	at javax.faces.api@3.0.0.SP04//javax.faces.component.UIComponentBase.processValidators(UIComponentBase.java:921)
 	at javax.faces.api@3.0.0.SP04//javax.faces.component.UIComponentBase.processValidators(UIComponentBase.java:921)
 	at javax.faces.api@3.0.0.SP04//javax.faces.component.UIForm.processValidators(UIForm.java:229)
 	at javax.faces.api@3.0.0.SP04//javax.faces.component.UIComponentBase.processValidators(UIComponentBase.java:921)
 	at deployment.DivingReservationEAR.ear.DivingReservationWebPrimeFace.war//org.primefaces.component.dialog.Dialog.processValidators(Dialog.java:152)
 	at javax.faces.api@3.0.0.SP04//javax.faces.component.UIComponentBase.processValidators(UIComponentBase.java:921)
 	at javax.faces.api@3.0.0.SP04//javax.faces.component.UIComponentBase.processValidators(UIComponentBase.java:921)
 	at deployment.DivingReservationEAR.ear.DivingReservationWebPrimeFace.war//org.primefaces.extensions.component.layout.Layout.processValidators(Layout.java:249)
	at javax.faces.api@3.0.0.SP04//javax.faces.component.UIComponentBase.processValidators(UIComponentBase.java:921)
	at javax.faces.api@3.0.0.SP04//javax.faces.component.UIComponentBase.processValidators(UIComponentBase.java:921)
	at javax.faces.api@3.0.0.SP04//javax.faces.component.UIViewRoot.processValidators(UIViewRoot.java:1310)
	at com.sun.jsf-impl@2.3.14.SP04//com.sun.faces.lifecycle.ProcessValidationsPhase.execute(ProcessValidationsPhase.java:53)
	at com.sun.jsf-impl@2.3.14.SP04//com.sun.faces.lifecycle.Phase.doPhase(Phase.java:76)
	at com.sun.jsf-impl@2.3.14.SP04//com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:177)
	at javax.faces.api@3.0.0.SP04//javax.faces.webapp.FacesServlet.executeLifecyle(FacesServlet.java:707)
	at javax.faces.api@3.0.0.SP04//javax.faces.webapp.FacesServlet.service(FacesServlet.java:451)
	at io.undertow.servlet@2.2.12.Final//io.undertow.servlet.handlers.ServletHandler.handleRequest(ServletHandler.java:74)
	at io.undertow.servlet@2.2.12.Final//io.undertow.servlet.handlers.FilterHandler$FilterChainImpl.doFilter(FilterHandler.java:129)
	at io.opentracing.contrib.opentracing-jaxrs2//io.opentracing.contrib.jaxrs2.server.SpanFinishingFilter.doFilter(SpanFinishingFilter.java:52)
	at io.undertow.servlet@2.2.12.Final//io.undertow.servlet.core.ManagedFilter.doFilter(ManagedFilter.java:61)
	at io.undertow.servlet@2.2.12.Final//io.undertow.servlet.handlers.FilterHandler$FilterChainImpl.doFilter(FilterHandler.java:131)
	at io.undertow.servlet@2.2.12.Final//io.undertow.servlet.handlers.FilterHandler.handleRequest(FilterHandler.java:84)
	at io.undertow.servlet@2.2.12.Final//io.undertow.servlet.handlers.security.ServletSecurityRoleHandler.handleRequest(ServletSecurityRoleHandler.java:62)
	at io.undertow.servlet@2.2.12.Final//io.undertow.servlet.handlers.ServletChain$1.handleRequest(ServletChain.java:68)
	at io.undertow.servlet@2.2.12.Final//io.undertow.servlet.handlers.ServletDispatchingHandler.handleRequest(ServletDispatchingHandler.java:36)
	at org.wildfly.security.elytron-web.undertow-server@1.10.0.Final//org.wildfly.elytron.web.undertow.server.ElytronRunAsHandler.lambda$handleRequest$1(ElytronRunAsHandler.java:68)
	at org.wildfly.security.elytron-base@1.17.1.Final//org.wildfly.security.auth.server.FlexibleIdentityAssociation.runAsFunctionEx(FlexibleIdentityAssociation.java:103)
	at org.wildfly.security.elytron-base@1.17.1.Final//org.wildfly.security.auth.server.Scoped.runAsFunctionEx(Scoped.java:161)
	at org.wildfly.security.elytron-base@1.17.1.Final//org.wildfly.security.auth.server.Scoped.runAs(Scoped.java:73)
	at org.wildfly.security.elytron-web.undertow-server@1.10.0.Final//org.wildfly.elytron.web.undertow.server.ElytronRunAsHandler.handleRequest(ElytronRunAsHandler.java:67)
	at io.undertow.servlet@2.2.12.Final//io.undertow.servlet.handlers.RedirectDirHandler.handleRequest(RedirectDirHandler.java:68)
	at io.undertow.servlet@2.2.12.Final//io.undertow.servlet.handlers.security.SSLInformationAssociationHandler.handleRequest(SSLInformationAssociationHandler.java:117)
	at io.undertow.servlet@2.2.12.Final//io.undertow.servlet.handlers.security.ServletAuthenticationCallHandler.handleRequest(ServletAuthenticationCallHandler.java:57)
	at io.undertow.core@2.2.12.Final//io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
	at io.undertow.core@2.2.12.Final//io.undertow.security.handlers.AbstractConfidentialityHandler.handleRequest(AbstractConfidentialityHandler.java:46)
	at io.undertow.servlet@2.2.12.Final//io.undertow.servlet.handlers.security.ServletConfidentialityConstraintHandler.handleRequest(ServletConfidentialityConstraintHandler.java:64)
	at io.undertow.core@2.2.12.Final//io.undertow.security.handlers.AbstractSecurityContextAssociationHandler.handleRequest(AbstractSecurityContextAssociationHandler.java:43)
	at org.wildfly.security.elytron-web.undertow-server-servlet@1.10.0.Final//org.wildfly.elytron.web.undertow.server.servlet.CleanUpHandler.handleRequest(CleanUpHandler.java:38)
	at io.undertow.core@2.2.12.Final//io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
	at org.wildfly.extension.undertow@25.0.1.Final//org.wildfly.extension.undertow.security.jacc.JACCContextIdHandler.handleRequest(JACCContextIdHandler.java:61)
	at io.undertow.core@2.2.12.Final//io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
	at org.wildfly.extension.undertow@25.0.1.Final//org.wildfly.extension.undertow.deployment.GlobalRequestControllerHandler.handleRequest(GlobalRequestControllerHandler.java:68)
	at io.undertow.servlet@2.2.12.Final//io.undertow.servlet.handlers.SendErrorPageHandler.handleRequest(SendErrorPageHandler.java:52)
	at io.undertow.core@2.2.12.Final//io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
	at io.undertow.servlet@2.2.12.Final//io.undertow.servlet.handlers.ServletInitialHandler.handleFirstRequest(ServletInitialHandler.java:280)
	at io.undertow.servlet@2.2.12.Final//io.undertow.servlet.handlers.ServletInitialHandler.access$100(ServletInitialHandler.java:79)
	at io.undertow.servlet@2.2.12.Final//io.undertow.servlet.handlers.ServletInitialHandler$2.call(ServletInitialHandler.java:134)
	at io.undertow.servlet@2.2.12.Final//io.undertow.servlet.handlers.ServletInitialHandler$2.call(ServletInitialHandler.java:131)
	at io.undertow.servlet@2.2.12.Final//io.undertow.servlet.core.ServletRequestContextThreadSetupAction$1.call(ServletRequestContextThreadSetupAction.java:48)
	at io.undertow.servlet@2.2.12.Final//io.undertow.servlet.core.ContextClassLoaderSetupAction$1.call(ContextClassLoaderSetupAction.java:43)
	at org.wildfly.extension.undertow@25.0.1.Final//org.wildfly.extension.undertow.deployment.UndertowDeploymentInfoService$UndertowThreadSetupAction.lambda$create$0(UndertowDeploymentInfoService.java:1544)
	at org.wildfly.extension.undertow@25.0.1.Final//org.wildfly.extension.undertow.deployment.UndertowDeploymentInfoService$UndertowThreadSetupAction.lambda$create$0(UndertowDeploymentInfoService.java:1544)
	at org.wildfly.extension.undertow@25.0.1.Final//org.wildfly.extension.undertow.deployment.UndertowDeploymentInfoService$UndertowThreadSetupAction.lambda$create$0(UndertowDeploymentInfoService.java:1544)
	at org.wildfly.extension.undertow@25.0.1.Final//org.wildfly.extension.undertow.deployment.UndertowDeploymentInfoService$UndertowThreadSetupAction.lambda$create$0(UndertowDeploymentInfoService.java:1544)
	at org.wildfly.extension.undertow@25.0.1.Final//org.wildfly.extension.undertow.deployment.UndertowDeploymentInfoService$UndertowThreadSetupAction.lambda$create$0(UndertowDeploymentInfoService.java:1544)
	at io.undertow.servlet@2.2.12.Final//io.undertow.servlet.handlers.ServletInitialHandler.dispatchRequest(ServletInitialHandler.java:260)
	at io.undertow.servlet@2.2.12.Final//io.undertow.servlet.handlers.ServletInitialHandler.access$000(ServletInitialHandler.java:79)
	at io.undertow.servlet@2.2.12.Final//io.undertow.servlet.handlers.ServletInitialHandler$1.handleRequest(ServletInitialHandler.java:100)
	at io.undertow.core@2.2.12.Final//io.undertow.server.Connectors.executeRootHandler(Connectors.java:387)
	at io.undertow.core@2.2.12.Final//io.undertow.server.HttpServerExchange$1.run(HttpServerExchange.java:852)
	at org.jboss.threads@2.4.0.Final//org.jboss.threads.ContextClassLoaderSavingRunnable.run(ContextClassLoaderSavingRunnable.java:35)
	at org.jboss.threads@2.4.0.Final//org.jboss.threads.EnhancedQueueExecutor.safeRun(EnhancedQueueExecutor.java:1990)
	at org.jboss.threads@2.4.0.Final//org.jboss.threads.EnhancedQueueExecutor$ThreadBody.doRunTask(EnhancedQueueExecutor.java:1486)
	at org.jboss.threads@2.4.0.Final//org.jboss.threads.EnhancedQueueExecutor$ThreadBody.run(EnhancedQueueExecutor.java:1377)
	at org.jboss.xnio@3.8.4.Final//org.xnio.XnioWorker$WorkerThreadFactory$1$1.run(XnioWorker.java:1280)
	at java.base/java.lang.Thread.run(Thread.java:833)

Even if I the only field with a list in my object is already configured with a fetch strategie = FetchType.EAGER and the JPA transaction for hibernet is set to true.

I probably missed something, but what ?
Can someone help?

You will have to figure out why the collection is not initialized. With the limited code and error information you have given it’s impossible to figure out what is going on.

Thank you for your reponse.

Regarding what I read on internet about this kind of error, the solution is globally to set the fetch strategie to EAGER, witch in my case don’t solve the problème.

I can give more informations but the error don’t occured in my own code.
It occurred durring the PROCESS_VALIDATIONS phase of JSF. During this phase, JSF convert and validate the posted value, and then, put the SubmittedValue in the field Value of the UIComponent.
The content of the Value field come from the entityBean used to displayed the contenent ot the UIComponent, but should be detached since the previous transaction was closed. This is managed by the EE container who should close the EntityManager between each web request.

I don’t undestand why the exception is thrown. Is it because there is not opened transaction? In what kind of situation a collection is not initialized? In this case, I suppose the collection was initialized during the first request, while fetching the persistedBean and it’s collection.

Did this help ?

If you debug a bit into the JSF code you will likely notice that for a selectManyMenu JSF tries to create a clone of the current collection value: mojarra/MenuRenderer.java at master · eclipse-ee4j/mojarra · GitHub

The default constructor produces an “uninitialized” collection though which fails on every method call. You can either provide a collection type hint: jsf - selectManyCheckbox LazyInitializationException on process validation - Stack Overflow

Or you use a DTO model and apply the values to your entities only when you actually save the object.

I don’t know why I didn’t find the StackOverflow’s Page.

My reflexion send me to a work around consisting to wrap the getter with another method and copie the PersistentSet to a new HashSet. This works, but the solution you brought to me is really better.

Thank a lot for your answer.