11 Replies Latest reply on Dec 14, 2011 4:57 AM by Nikola Kartelija

    Annother LazyInitializationException

    Nikola Kartelija Newbie

      I am having troubles with LazyInitializationException


      Here is a piece of code


      @Named("evaluation")
      @ViewScoped
      public class EvaluationAction implements Serializable {
           @PersistenceContext
           private transient EntityManager entityManager;
      
           public boolean fetchQuestionnaire() {
                questionnaire = entityManager.find(Questionnaire.class, pageParams.getQuestionnaireId());
                      String id = questionnaire.getSubject().getId(); 
                      String name = questionnaire.getSubject().getFirstName() //this is the line a LazyInitializationException is thrown
      ...
                return true;
           }
      }



      I tried to add @TransactionAttribute to the method with no success. I tried this because I have another bean that works and goes something like this:




      @Named("questionnaireAction")
      @ViewScoped
      public class QuestionnaireAction implements Serializable {
      
           .....
           @TransactionAttribute
           public String loadQuestionnaire() {
                   ...
                Questionnaire questionnaire = entityManager.find(Questionnaire.class, pageParams.getQuestionnaireId());
                subjectId = questionnaire.getSubject().getId();
      
                //Step One
                firstName = questionnaire.getSubject().getFirstName();
                middleName = questionnaire.getSubject().getMiddleName();
                lastName = questionnaire.getSubject().getLastName();
                idNumber = questionnaire.getSubject().getPatientId();
                dateOfBirth = questionnaire.getSubject().getDateOfBirth();
                     ...
              }
      }



      The above code gave me the same LazyLoadingException problem until i added @TransactionAttribute. If anyone has any idea about this problem and how to solve it, please share your knowledge.


        • 1. Re: Annother LazyInitializationException
          Jason Porter Master

          Which annotations are you using? Also, why not use the Seam Managed Persistence Context? Also may need to see some more code, the entities perhaps That LIE is very odd as there's nothing there that shouldn't be current to the entityManager.

          • 2. Re: Annother LazyInitializationException
            Nikola Kartelija Newbie

            Here is the PersistenceConfig class




            public class PersistenceConfig {
                @ExtensionManaged
                @Produces
                @PersistenceUnit
                @ConversationScoped
                @Default
                  EntityManagerFactory producerField;
            }
            



            the part of the Questionnaire entity




            @Table(name = "questionnaire")
            @Entity
            @Veto
            public class Questionnaire extends BasicEntity implements Serializable{
                 private Study study;
                 private Subject subject;
            
                 private Date dateCompleted;
            
            ...
            
                 @ManyToOne(fetch = FetchType.LAZY)
                 @JoinColumn(name = "study_id")
                 public Study getStudy() {
                      return study;
                 }
            
                 public void setStudy(Study study) {
                      this.study = study;
                 }
            
                 @ManyToOne(fetch = FetchType.LAZY)
                 @JoinColumn(name = "subject_id")
                 public Subject getSubject() {
                      return subject;
                 }
            
                 public void setSubject(Subject subject) {
                      this.subject = subject;
                 }
            
                 @Column(name = "questionnaireDate")
                 public Date getDateCompleted() {
                      return dateCompleted;
                 }
            
                 public void setDateCompleted(Date dateCompleted) {
                      this.dateCompleted = dateCompleted;
                 }
            }
            



            and the part of the Subject entity




            @Table(name = "subject")
            @Entity
            @Veto
            public class Subject extends BasicEntity implements Serializable{
            
                 private String firstName;
                 private String lastName;
                    ...
            
                 public String getFirstName() {
                      return firstName;
                 }
            
                 public void setFirstName(String firstName) {
                      this.firstName = firstName;
                 }
            
            
                 public String getLastName() {
                      return lastName;
                 }
            
                 public void setLastName(String lastName) {
                      this.lastName = lastName;
                 }
            
            


            The BasicEntity holds only ID and creationDate columns. Yes, this LIE is very strange. I am at a total loss and really don't know what to do about this. Any help is greatly appreciated.





            • 3. Re: Annother LazyInitializationException
              Jason Porter Master

              Would you post the stack trace please?

              • 4. Re: Annother LazyInitializationException
                Nikola Kartelija Newbie

                Here is the stack trace




                15:48:08,453 ERROR [org.apache.catalina.core.ContainerBase.[jboss.web].[default-host].[/sleep-profiler].[Faces Servlet]] (http--127.0.0.1-8080-1) Servlet.service() for servlet Faces Servlet threw exception: org.hibernate.LazyInitializationException: could not initialize proxy - no Session
                     at org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:149) [hibernate-core-4.0.0.CR2.jar:4.0.0.CR2]
                     at org.hibernate.proxy.AbstractLazyInitializer.getImplementation(AbstractLazyInitializer.java:195) [hibernate-core-4.0.0.CR2.jar:4.0.0.CR2]
                     at org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer.invoke(JavassistLazyInitializer.java:180) [hibernate-core-4.0.0.CR2.jar:4.0.0.CR2]
                     at com.abm.sleep.profiler.database.model.Subject_$$_javassist_56.getFirstName(Subject_$$_javassist_56.java) [sleep-profiler-datamodel-1.0-SNAPSHOT.jar:]
                     at com.abm.sleep.profiler.business.logic.action.members.EvaluationAction.fetchQuestionnaire(EvaluationAction.java:119) [sleep-profiler-business-logic-1.0-SNAPSHOT.jar:]
                     at com.abm.sleep.profiler.business.logic.action.members.EvaluationAction.loadQuestionnaire(EvaluationAction.java:92) [sleep-profiler-business-logic-1.0-SNAPSHOT.jar:]
                     at com.abm.sleep.profiler.business.logic.action.members.-1480591165$Proxy$_$$_WeldSubclass.loadQuestionnaire(-1480591165$Proxy$_$$_WeldSubclass.java) [sleep-profiler-business-logic-1.0-SNAPSHOT.jar:]
                     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) [:1.6.0_24]
                     at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) [:1.6.0_24]
                     at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) [:1.6.0_24]
                     at java.lang.reflect.Method.invoke(Method.java:597) [:1.6.0_24]
                     at org.jboss.interceptor.proxy.SimpleInterceptionChain.invokeNextInterceptor(SimpleInterceptionChain.java:112) [jboss-interceptor-core-2.0.0.Alpha3.jar:2.0.0.Alpha3]
                     at org.jboss.interceptor.proxy.InterceptorMethodHandler.executeInterception(InterceptorMethodHandler.java:133) [jboss-interceptor-core-2.0.0.Alpha3.jar:2.0.0.Alpha3]
                     at org.jboss.interceptor.proxy.InterceptorMethodHandler.invoke(InterceptorMethodHandler.java:112) [jboss-interceptor-core-2.0.0.Alpha3.jar:2.0.0.Alpha3]
                     at org.jboss.weld.bean.proxy.CombinedInterceptorAndDecoratorStackMethodHandler.invoke(CombinedInterceptorAndDecoratorStackMethodHandler.java:65) [weld-core-1.1.2.Final.jar:2011-07-26 15:02]
                     at com.abm.sleep.profiler.business.logic.action.members.-1480591165$Proxy$_$$_WeldSubclass.loadQuestionnaire(-1480591165$Proxy$_$$_WeldSubclass.java) [sleep-profiler-business-logic-1.0-SNAPSHOT.jar:]
                     at com.abm.sleep.profiler.business.logic.action.members.EvaluationAction$Proxy$_$$_WeldClientProxy.loadQuestionnaire(EvaluationAction$Proxy$_$$_WeldClientProxy.java) [sleep-profiler-business-logic-1.0-SNAPSHOT.jar:]
                     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) [:1.6.0_24]
                     at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) [:1.6.0_24]
                     at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) [:1.6.0_24]
                     at java.lang.reflect.Method.invoke(Method.java:597) [:1.6.0_24]
                     at org.apache.el.parser.AstValue.invoke(AstValue.java:196) [jbossweb-7.0.1.Final.jar:7.0.2.Final]
                     at org.apache.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:276) [jbossweb-7.0.1.Final.jar:7.0.2.Final]
                     at org.jboss.weld.util.el.ForwardingMethodExpression.invoke(ForwardingMethodExpression.java:43) [weld-core-1.1.2.Final.jar:2011-07-26 15:02]
                     at org.jboss.weld.el.WeldMethodExpression.invoke(WeldMethodExpression.java:56) [weld-core-1.1.2.Final.jar:2011-07-26 15:02]
                     at com.ocpsoft.pretty.faces.util.FacesElUtils.invokeMethod(FacesElUtils.java:70) [prettyfaces-jsf2-3.3.0.jar:]
                     at com.ocpsoft.pretty.faces.util.FacesElUtils.invokeMethod(FacesElUtils.java:62) [prettyfaces-jsf2-3.3.0.jar:]
                     at com.ocpsoft.pretty.faces.beans.ActionExecutor.executeActions(ActionExecutor.java:56) [prettyfaces-jsf2-3.3.0.jar:]
                     at com.ocpsoft.pretty.faces.event.PrettyPhaseListener.processEvent(PrettyPhaseListener.java:123) [prettyfaces-jsf2-3.3.0.jar:]
                     at com.ocpsoft.pretty.faces.event.PrettyPhaseListener.afterPhase(PrettyPhaseListener.java:110) [prettyfaces-jsf2-3.3.0.jar:]
                     at com.sun.faces.lifecycle.Phase.handleAfterPhase(Phase.java:189) [jsf-impl-2.1.3-b02-jbossorg-2.jar:2.1.3-SNAPSHOT]
                     at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:107) [jsf-impl-2.1.3-b02-jbossorg-2.jar:2.1.3-SNAPSHOT]
                     at com.sun.faces.lifecycle.RestoreViewPhase.doPhase(RestoreViewPhase.java:116) [jsf-impl-2.1.3-b02-jbossorg-2.jar:2.1.3-SNAPSHOT]
                     at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118) [jsf-impl-2.1.3-b02-jbossorg-2.jar:2.1.3-SNAPSHOT]
                     at javax.faces.webapp.FacesServlet.service(FacesServlet.java:593) [jboss-jsf-api_2.1_spec-2.0.0.Beta1.jar:2.0.0.Beta1]
                     at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:329) [jbossweb-7.0.1.Final.jar:7.0.2.Final]
                     at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:248) [jbossweb-7.0.1.Final.jar:7.0.2.Final]
                     at com.ocpsoft.pretty.PrettyFilter.doFilter(PrettyFilter.java:118) [prettyfaces-jsf2-3.3.0.jar:]
                     at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:280) [jbossweb-7.0.1.Final.jar:7.0.2.Final]
                     at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:248) [jbossweb-7.0.1.Final.jar:7.0.2.Final]
                     at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:734) [jbossweb-7.0.1.Final.jar:7.0.2.Final]
                     at org.apache.catalina.core.ApplicationDispatcher.processRequest(ApplicationDispatcher.java:541) [jbossweb-7.0.1.Final.jar:7.0.2.Final]
                     at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:479) [jbossweb-7.0.1.Final.jar:7.0.2.Final]
                     at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:407) [jbossweb-7.0.1.Final.jar:7.0.2.Final]
                     at com.ocpsoft.pretty.PrettyFilter.doFilter(PrettyFilter.java:110) [prettyfaces-jsf2-3.3.0.jar:]
                     at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:280) [jbossweb-7.0.1.Final.jar:7.0.2.Final]
                     at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:248) [jbossweb-7.0.1.Final.jar:7.0.2.Final]
                     at org.jboss.solder.servlet.exception.CatchExceptionFilter.doFilter(CatchExceptionFilter.java:65) [solder-impl-3.1.0.Beta5.jar:]
                     at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:280) [jbossweb-7.0.1.Final.jar:7.0.2.Final]
                     at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:248) [jbossweb-7.0.1.Final.jar:7.0.2.Final]
                     at org.jboss.solder.servlet.event.ServletEventBridgeFilter.doFilter(ServletEventBridgeFilter.java:74) [solder-impl-3.1.0.Beta5.jar:]
                     at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:280) [jbossweb-7.0.1.Final.jar:7.0.2.Final]
                     at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:248) [jbossweb-7.0.1.Final.jar:7.0.2.Final]
                     at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:275) [jbossweb-7.0.1.Final.jar:7.0.2.Final]
                     at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:161) [jbossweb-7.0.1.Final.jar:7.0.2.Final]
                     at org.jboss.as.web.security.SecurityContextAssociationValve.invoke(SecurityContextAssociationValve.java:139) [jboss-as-web-7.0.2.Final.jar:7.0.2.Final]
                     at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:388) [jbossweb-7.0.1.Final.jar:7.0.2.Final]
                     at org.jboss.as.web.NamingValve.invoke(NamingValve.java:57) [jboss-as-web-7.0.2.Final.jar:7.0.2.Final]
                     at org.jboss.as.jpa.interceptor.WebNonTxEmCloserValve.invoke(WebNonTxEmCloserValve.java:49) [jboss-as-jpa-7.0.2.Final.jar:7.0.2.Final]
                     at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:154) [jbossweb-7.0.1.Final.jar:7.0.2.Final]
                     at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102) [jbossweb-7.0.1.Final.jar:7.0.2.Final]
                     at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109) [jbossweb-7.0.1.Final.jar:7.0.2.Final]
                     at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:362) [jbossweb-7.0.1.Final.jar:7.0.2.Final]
                     at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:877) [jbossweb-7.0.1.Final.jar:7.0.2.Final]
                     at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:667) [jbossweb-7.0.1.Final.jar:7.0.2.Final]
                     at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:952) [jbossweb-7.0.1.Final.jar:7.0.2.Final]
                     at java.lang.Thread.run(Thread.java:662) [:1.6.0_24]



                • 5. Re: Annother LazyInitializationException
                  hantsy bai Master

                  I also encountered LazyInitializationException before, I think I had posted it in this Forum, I did not remember if I had reported it as a bug. Different from here, I used Seam Managed Persistence...and used @Inject to inject EM in my bean.


                  After I switched the Bean to ConversationScoped, the exception was disappeared.


                  • 6. Re: Annother LazyInitializationException
                    Nikola Kartelija Newbie

                    When I try to use @Inject for the EntityManager strange things start to happen. I have a DatabaseManager @Stateless class in which I can inject the em, but in Questionnaire, for example, IDEA tels me that there are no satisfied dependencies !? and when I try to deploy the application I get the folowing exception:




                    15:02:20,812 ERROR [org.jboss.msc.service.fail] (MSC service thread 1-3) MSC00001: Failed to start service jboss.deployment.unit."sleep-profiler-web-1.0-SNAPSHOT.war".WeldService: org.jboss.msc.service.StartException in service jboss.deployment.unit."sleep-profiler-web-1.0-SNAPSHOT.war".WeldService: org.jboss.weld.exceptions.DeploymentException: WELD-001408 Unsatisfied dependencies for type [EntityManager] with qualifiers [@Default] at injection point [[field] @Inject private transient com.abm.sleep.profiler.business.logic.action.members.EvaluationAction.entityManager]
                            at org.jboss.as.weld.services.WeldService.start(WeldService.java:96)
                            at org.jboss.msc.service.ServiceControllerImpl$StartTask.startService(ServiceControllerImpl.java:1824) [jboss-msc-1.0.1.GA.jar:1.0.1.GA]
                            at org.jboss.msc.service.ServiceControllerImpl$StartTask.run(ServiceControllerImpl.java:1759) [jboss-msc-1.0.1.GA.jar:1.0.1.GA]
                            at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886) [:1.6.0_24]
                            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908) [:1.6.0_24]
                            at java.lang.Thread.run(Thread.java:662) [:1.6.0_24]
                    Caused by: org.jboss.weld.exceptions.DeploymentException: WELD-001408 Unsatisfied dependencies for type [EntityManager] with qualifiers [@Default] at injection point [[field] @Inject private transient com.abm.sleep.profiler.business.logic.action.members.EvaluationAction.entityManager]
                            at org.jboss.weld.bootstrap.Validator.validateInjectionPoint(Validator.java:270)
                            at org.jboss.weld.bootstrap.Validator.validateBean(Validator.java:106)
                            at org.jboss.weld.bootstrap.Validator.validateRIBean(Validator.java:129)
                            at org.jboss.weld.bootstrap.Validator.validateBeans(Validator.java:351)
                            at org.jboss.weld.bootstrap.Validator.validateDeployment(Validator.java:336)
                            at org.jboss.weld.bootstrap.WeldBootstrap.validateBeans(WeldBootstrap.java:404)
                            at org.jboss.as.weld.WeldContainer.start(WeldContainer.java:82)
                            at org.jboss.as.weld.services.WeldService.start(WeldService.java:89)
                            ... 5 more








                    • 7. Re: Annother LazyInitializationException
                      Nikola Kartelija Newbie

                      Sorry, it seams that DatabaseManager class throws the same exception at deploy. I am really confused.

                      • 8. Re: Annother LazyInitializationException
                        Jason Porter Master

                        Is your project publicly available, or maybe a small zip of what you have?

                        • 9. Re: Annother LazyInitializationException
                          Nikola Kartelija Newbie

                          I'm sorry, but it isn't publicly available and I'm not allowed to share it.

                          • 10. Re: Annother LazyInitializationException
                            Jason Porter Master

                            Okay, that just makes it harder to help, but not impossible. Could you reproduce with a smaller project, or at least post of some code (cleaned up / obfuscated if need be)?