1 2 3 Previous Next 33 Replies Latest reply on Feb 1, 2013 10:52 AM by blabno

    How to avoid LazyInitializationException in facelets page.

    hantsy

      I have a Many to many relation between two entities, Addressee and AddresseeType.


           @ManyToMany
           private List<AddresseeType> addresseeTypeCollection = new ArrayList<AddresseeType>();
      
      



      in the AddresseeEdit page, I used a multi checkbox to select AddressType.


      <p:input id="addresseeType"
                                         label="#{messages['Addressee.AddressType']}">
                                         <h:selectManyCheckbox id="input"
                                              value="#{addresseeEdit.currentAddressee.addresseeTypeCollection}"
                                              required="true" layout="pageDirection"
                                              styleClass="checkboxInput">
                                              <f:selectItems var="atVar" itemLabel="#{atVar.addresseeType}"
                                                   value="#{addresseeTypes}" />
                                              <f:converter converterId="addresseeTypeConverter" />
                                         </h:selectManyCheckbox>
                                    </p:input>
      



      In before seam 2 application, this is not a problem.


      But now I am using Seam 3(EJB), how to avoid this issue?




        • 1. Re: How to avoid LazyInitializationException in facelets page.
          lightguard

          Use a Seam Managed Persistence context that is ConversationScoped.

          • 2. Re: How to avoid LazyInitializationException in facelets page.
            hantsy

            But in the documentation, it suggested to use @PersistenceContext when used EJB3(I used EJB3.1 none interface bean in our application) and use a ConversationScope EntityManager Producer when used none EJB environment.


            In the Seam 2, there is a two transaction phase in one jsf request...this is not available in Seam 3?


            I found in the Seam Persistence document, currently, when used ConversationScope EntityManager Producer, there are some shortness of EJB transaction.


            I hope Seam 3 can provide a united, seamless transaction solution for user, allow user use EJB or none EJB freely and there is no gap switch one to another.


            Thanks.



            • 3. Re: How to avoid LazyInitializationException in facelets page.
              swd847

              Seam managed transactions are provided by the Seam Faces module.


              Where in the documentation did it say to use @PersistenceContext? To avoid a LIE you should use the seam managed persistence context instead.

              • 4. Re: How to avoid LazyInitializationException in facelets page.
                hantsy

                I can  not find it in the Seam faces, but in Seam Persistence module.

                In the Seam 3.1.0.beta2, I found a standalone Transcation module.

                In the Seam persistence document, Seam managed persistence can not take over everything of EJB transaction in the EE container.

                For example, the exception is not united, the it does not support REQUIRE
                NEW.



                I will try the Seam managed persistence.


                Thank u all.

                • 5. Re: How to avoid LazyInitializationException in facelets page.
                  hantsy

                  I added Seam managed persistence now.


                  The lazyinitializedException was disappeared when the page was loading.
                  But when I clicked the submit button, a lazyinitializedException was occured.


                  org.hibernate.LazyInitializationException: failed to lazily initialize a collection, no session or session was closed
                       at org.hibernate.collection.internal.AbstractPersistentCollection.throwLazyInitializationException(AbstractPersistentCollection.java:393)
                       at org.hibernate.collection.internal.AbstractPersistentCollection.throwLazyInitializationExceptionIfNotConnected(AbstractPersistentCollection.java:385)
                       at org.hibernate.collection.internal.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:378)
                       at org.hibernate.collection.internal.AbstractPersistentCollection.write(AbstractPersistentCollection.java:208)
                       at org.hibernate.collection.internal.PersistentBag.add(PersistentBag.java:292)
                       at com.sun.faces.renderkit.html_basic.MenuRenderer.convertSelectManyValuesForModel(MenuRenderer.java:382)
                       at com.sun.faces.renderkit.html_basic.MenuRenderer.convertSelectManyValue(MenuRenderer.java:129)
                       at com.sun.faces.renderkit.html_basic.MenuRenderer.getConvertedValue(MenuRenderer.java:315)
                       at javax.faces.component.UIInput.getConvertedValue(UIInput.java:1028)
                       at javax.faces.component.UIInput.validate(UIInput.java:958)
                       at javax.faces.component.UIInput.executeValidate(UIInput.java:1209)
                       at javax.faces.component.UIInput.processValidators(UIInput.java:698)
                       at javax.faces.component.UIComponentBase.processValidators(UIComponentBase.java:1085)
                       at javax.faces.component.UIComponentBase.processValidators(UIComponentBase.java:1085)
                       at javax.faces.component.UIForm.processValidators(UIForm.java:244)
                       at javax.faces.component.UIComponentBase.processValidators(UIComponentBase.java:1085)
                       at javax.faces.component.UIComponentBase.processValidators(UIComponentBase.java:1085)
                       at javax.faces.component.UIComponentBase.processValidators(UIComponentBase.java:1085)
                       at javax.faces.component.UIViewRoot.processValidators(UIViewRoot.java:1165)
                       at com.sun.faces.lifecycle.ProcessValidationsPhase.execute(ProcessValidationsPhase.java:76)
                       at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
                       at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118)
                       at javax.faces.webapp.FacesServlet.service(FacesServlet.java:312)
                       at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:329)
                       at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:248)
                       at org.jboss.weld.servlet.ConversationPropagationFilter.doFilter(ConversationPropagationFilter.java:67)
                       at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:280)
                       at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:248)
                       at com.ocpsoft.pretty.PrettyFilter.doFilter(PrettyFilter.java:118)
                       at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:280)
                       at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:248)
                       at org.jboss.seam.servlet.exception.CatchExceptionFilter.doFilter(CatchExceptionFilter.java:65)
                       at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:280)
                       at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:248)
                       at org.jboss.seam.servlet.event.ServletEventBridgeFilter.doFilter(ServletEventBridgeFilter.java:72)
                       at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:280)
                       at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:248)
                       at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:275)
                       at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:161)
                       at org.jboss.as.web.security.SecurityContextAssociationValve.invoke(SecurityContextAssociationValve.java:139)
                       at org.jboss.as.web.NamingValve.invoke(NamingValve.java:57)
                       at org.jboss.as.jpa.interceptor.WebNonTxEmCloserValve.invoke(WebNonTxEmCloserValve.java:49)
                       at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:154)
                       at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
                       at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
                       at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:362)
                       at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:877)
                       at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:667)
                       at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:952)
                       at java.lang.Thread.run(Thread.java:662)
                  

                  • 6. Re: How to avoid LazyInitializationException in facelets page.
                    ssachtleben.ssachtleben.gmail.com

                    Well just fetch the collection before page load should fix it.

                    • 7. Re: How to avoid LazyInitializationException in facelets page.
                      hantsy

                      I used Seam managed persistence now.


                      It did not occur when page was rendered, but occured when I submited the form by click the save button.


                      In before seam 2 version, it worked well with a s:entityConverter


                      Now I created a customsized Conveter for it.


                      I tried to migrate some code in my seam 2 application to Seam3 in these two weeks, it seems very difficult to migrate it smoothly.

                      • 8. Re: How to avoid LazyInitializationException in facelets page.
                        lightguard

                        Are you in a long running conversation?

                        • 9. Re: How to avoid LazyInitializationException in facelets page.
                          hantsy

                          yes...


                          When fixed this issue.


                          Exception When Use Customsized Converter And Ajax In Seam3 Application


                          it disappeared, so strange.


                          • 10. Re: How to avoid LazyInitializationException in facelets page.
                            hantsy

                            find the problem.
                            When new , it does not persist the manyto many relation.
                            Wnen edit, modify the checkbox, the exception threw.


                            The complete code is here.


                            @Stateful
                            @ConversationScoped
                            @Named("addresseeEdit")
                            public class AddresseeEditAction {
                            
                                 private static final org.slf4j.Logger log = LoggerFactory
                                           .getLogger(AddresseeEditAction.class);
                            
                                 @Inject
                                 EntityManager em;
                            
                                 // @Inject
                                 // Logger log;
                            
                                 private Addressee currentAddressee;
                            
                                 @Inject
                                 private Event<Addressee> addresseeSavedEventSrc;
                            
                                 @Inject
                                 Messages messages;
                            
                                 @Inject
                                 Conversation conversation;
                            
                                 private Long addresseeId;
                            
                                 public Long getAddresseeId() {
                                      return addresseeId;
                                 }
                            
                                 public void setAddresseeId(Long addresseeId) {
                                      this.addresseeId = addresseeId;
                                 }
                            
                                 public Addressee getCurrentAddressee() {
                                      return currentAddressee;
                                 }
                            
                                 public void setCurrentAddressee(Addressee currentAddressee) {
                                      this.currentAddressee = currentAddressee;
                                 }
                            
                                 @PostConstruct
                                 public void init() {
                                      if (log.isDebugEnabled()) {
                                           log.debug("call init...");
                                      }
                                 }
                            
                                 public void select() {
                                      if (log.isDebugEnabled()) {
                                           log.debug("call select...@" + this.addresseeId);
                                      }
                            
                                      if (conversation.isTransient()) {
                                           conversation.begin();
                                      }
                            
                                      if (this.addresseeId != null) {
                                           this.currentAddressee = em.find(Addressee.class, this.addresseeId);
                                           if (this.currentAddressee != null
                                                     && this.currentAddressee.getAreaCode() != null) {
                                                this.selectedCountry = this.currentAddressee.getAreaCode()
                                                          .getCity().getCountry();
                                           }
                                      } else {
                                           this.currentAddressee = new Addressee();
                                      }
                            
                                 }
                            
                                 // @End
                                 @Transactional
                                 public void save() {
                                      if (log.isDebugEnabled()) {
                                           log.debug("call save...");
                                      }
                            
                                      if (this.currentAddressee.getId() == null) {
                                           em.persist(this.currentAddressee);
                                      } else {
                                           this.currentAddressee = em.merge(this.currentAddressee);
                                      }
                            
                                      this.addresseeSavedEventSrc.fire(this.currentAddressee);
                            
                                      if (!conversation.isTransient()) {
                                           conversation.end();
                                      }
                                 }
                            
                                 public void onSaved(
                                           @Observes(during = TransactionPhase.AFTER_SUCCESS) Addressee addressee) {
                                      messages.info(new DefaultBundleKey("addressee_saved"))
                                                .defaults("Addressee saved").params(addressee.getFullname());
                                 }
                            
                                 // @End
                                 public void cancel() {
                                      if (log.isDebugEnabled()) {
                                           log.debug("call end...");
                                      }
                            
                                      if (!conversation.isTransient()) {
                                           conversation.end();
                                      }
                            
                                 }
                            
                                 // -----------------dropdown box selection---------------
                                 private Country selectedCountry;
                            
                                 public Country getSelectedCountry() {
                                      return selectedCountry;
                                 }
                            
                                 public void setSelectedCountry(Country selectedCountry) {
                                      this.selectedCountry = selectedCountry;
                                 }
                            
                            
                                 @SuppressWarnings("unchecked")
                                 public List<City> getCities() {
                                      if (log.isDebugEnabled()) {
                                           log.debug("...getCities ... selectedCountry@"
                                                     + this.selectedCountry);
                                      }
                                      if (this.selectedCountry != null) {
                                           return em
                                                     .createQuery(
                                                               "select c from City c where c.country.id=:country")
                                                     .setParameter("country", this.selectedCountry.getId())
                                                     .getResultList();
                                      }
                            
                                      return Collections.<City> emptyList();
                            
                                 }
                            
                                 @SuppressWarnings("unchecked")
                                 public List<Port> getPorts() {
                                      if (log.isDebugEnabled()) {
                                           log.debug("...getPorts ...selectedCountry@" + this.selectedCountry);
                                      }
                                      if (this.selectedCountry != null) {
                                           return em
                                                     .createQuery(
                                                               "select c from Port c where c.country.id=:country")
                                                     .setParameter("country", this.selectedCountry.getId())
                                                     .getResultList();
                                      }
                            
                                      return Collections.<Port> emptyList();
                            
                                 }
                            
                                 @SuppressWarnings("unchecked")
                                 public List<CityAreaCode> getAreaCodes() {
                                      if (log.isDebugEnabled()) {
                                           log.debug("...getAreaCodes ...city@"
                                                     + this.currentAddressee.getCity());
                                      }
                                      if (this.currentAddressee.getCity() != null) {
                                           return em
                                                     .createQuery(
                                                               "select c from CityAreaCode c where c.city.id=:city")
                                                     .setParameter("city",
                                                               this.currentAddressee.getCity().getId())
                                                     .getResultList();
                                      }
                            
                                      return Collections.<CityAreaCode> emptyList();
                            
                                 }
                            
                            }
                            
                            

                            • 11. Re: How to avoid LazyInitializationException in facelets page.
                              hantsy

                              I am not sure which component in the page caused the problem

                              • 12. Re: How to avoid LazyInitializationException in facelets page.
                                hantsy

                                In before seam 2 application, I did not encountered such a problem

                                • 13. Re: How to avoid LazyInitializationException in facelets page.
                                  hantsy

                                  Any suggestion?

                                  • 14. Re: How to avoid LazyInitializationException in facelets page.
                                    ssachtleben.ssachtleben.gmail.com

                                    Which list throws that LazyInitializationException? Please post Addressee pojo.


                                    I would recommend to use criteria api and use joinList to fetch the list in select method.

                                    1 2 3 Previous Next