7 Replies Latest reply on Nov 24, 2009 9:25 AM by gcollin.gerardcollin.gmail.com

    convertEntity, lazy loading in view rendering and ejb transaction

    gcollin.gerardcollin.gmail.com
      Hello,

      I have some problems using Seam and EJB 3.0 beans in my project, and would like some help.

      I have the same web application running in 2 configurations: One is running Icefaces Seam JPA Hibernate under tomcat for development purpose.
      But using Maven magic, I can generate another delivery, using an ear, a war, an ejb-jar containing entities and stateless EJB 3 session beans. I then run this delivery under JBoss 4.2

      The application works fine in Tomcat, using a .war only. But in JBoss, I get some pages not working.

      I noticed to things using EJB configuration:
        - convertEntity throws big NullpointerException. I've seen in the doc,
      http://docs.jboss.org/seam/2.1.2/reference/en-US/html/controls.html#d0e28276, that I must configure the entitymanager for convertEntity to work. But with Ejb configuration, I don't have an entityManager managed by Seam, because it's the JBoss server that does the work.

        => It seems convertEntity don't work with EJBs, even 3.0 ? I can't find nowhere documentation for this.... And the EJB sample (booking), don't use convertEntity.....

        - Then, I have a page where I display data that is lazy-loaded. I mean Hibernate must do some more queries to get the data for display. While this works very well under Tomcat, I get lazyinit errors using Ejb.

        => Documentation says Seam manage the lazyloading stuff for display view, and it does for jpa entities, but does it do the same for EJB ?


      For info, here is my components.xml:

      <?xml version="1.0" encoding="UTF-8"?>
      <components xmlns="http://jboss.com/products/seam/components"
                  xmlns:core="http://jboss.com/products/seam/core"
                  xmlns:persistence="http://jboss.com/products/seam/persistence"
                  xmlns:transaction="http://jboss.com/products/seam/transaction"
                  xmlns:security="http://jboss.com/products/seam/security"
                  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                  xmlns:international="http://jboss.com/products/seam/international"
                  xmlns:ui="http://jboss.com/products/seam/ui"
                  xsi:schemaLocation=
                      "http://jboss.com/products/seam/ui http://jboss.com/products/seam/ui-2.1.xsd
                       http://jboss.com/products/seam/core http://jboss.com/products/seam/core-2.1.xsd
                       http://jboss.com/products/seam/persistence http://jboss.com/products/seam/persistence-2.1.xsd
                       http://jboss.com/products/seam/transaction http://jboss.com/products/seam/transaction-2.1.xsd
                       http://jboss.com/products/seam/security http://jboss.com/products/seam/security-2.1.xsd
                       http://jboss.com/products/seam/components http://jboss.com/products/seam/components-2.1.xsd
                       http://jboss.com/products/seam/international http://jboss.com/products/seam/international-2.1.xsd">

          <core:init debug="false" jndi-pattern="amanda2/#{ejbName}/local" />
          <core:ejb-transaction />
        
          <core:manager conversation-timeout="120000"
                        concurrent-request-timeout="500"
                        conversation-id-parameter="cid" />

          <security:identity authenticate-method="#{loginPage.checkLogin}" />
        
              <!-- Support for retrying the page after login -->
          <event type="org.jboss.seam.security.notLoggedIn">
              <action execute="#{redirect.captureCurrentView}"/>
          </event>

          <event type="org.jboss.seam.security.postAuthenticate">
              <action execute="#{redirect.returnToCapturedView}"/>
          </event>
      <!-- seam-components merge-point -->
      </components>
        • 1. Re: convertEntity, lazy loading in view rendering and ejb transaction
          kapitanpetko

          Even if you are using an EAR, you need to define the SMPC to use entity converter. For 'lazyloading stuff' you need the SMPC as well. Read more here


          • 2. Re: convertEntity, lazy loading in view rendering and ejb transaction
            gcollin.gerardcollin.gmail.com

            Thanks for your answer, I'll try to define one and see what happens.


            I'll keep you posted of the result.


            Regards,

            • 3. Re: convertEntity, lazy loading in view rendering and ejb transaction
              jguglielmin

              If you use seam-gen to create an ear project (selecting ICEfaces), then you will have the persistence set up properly (provided you answer the prompts correctly) and correct packaging.  Make sure you have all the ICEfaces jars in the ear lib folder.

              • 4. Re: convertEntity, lazy loading in view rendering and ejb transaction
                gcollin.gerardcollin.gmail.com
                Thank you all.

                I finally managed to get things working !

                In fact, I made much changes to the components.xml, following judy advice to use Seam-gen.

                For info, if other people encounters the same problems:

                  - Don't use <core:ejb-transaction /> In this case at least. That was creating double javassist proxies.....
                  - My business.jar was generated by Maven and contained
                  - I didn't put the jars in ear\lib directories, but directly in .ear
                  - icefaces was not in ear\lib but in ear\war\web-inf\lib
                  - Didn't configure properly persistence.xml (especially the JBoss jboss.entity.manager.factory.jndi.name config)
                  - Dao and Services were using @javax.persistence.PersistenceContext and not @In

                  - .... Lots of little mistakes like that.

                Having a working project generated by seam-gen hepled me a lot...

                Regards,
                • 5. Re: convertEntity, lazy loading in view rendering and ejb transaction
                  gcollin.gerardcollin.gmail.com
                  In fact, I just made some more testing, and I see that the problem was coming from the Dao and Service bean declaration.
                  I had to remove the javax.ejb.Local and javax.ejb.transaction declarations on my Dao, and I don't know why, because they should be Stateless Session ejb...

                  Regards,

                  This is NOT working (lazy init exceptions....):

                  @javax.ejb.TransactionAttribute(javax.ejb.TransactionAttributeType.REQUIRED)
                  @javax.ejb.Local({com.agcs.amd2.model.deal.DealDao.class})
                  public abstract class DealDaoBase
                      implements com.agcs.amd2.model.deal.DealDao
                  {
                      // ------ Session Context Injection ------
                     
                      @javax.annotation.Resource
                      protected javax.ejb.SessionContext context;

                      // ------ Persistence Context Injection --------
                     
                      @org.jboss.seam.annotations.In("entityManager")
                                  private javax.persistence.EntityManager emanager;
                  ...
                  }

                  And this is working:
                  package com.agcs.amd2.model.deal;

                  public abstract class DealDaoBase
                      implements com.agcs.amd2.model.deal.DealDao
                  {
                      // ------ Session Context Injection ------
                     
                      @javax.annotation.Resource
                      protected javax.ejb.SessionContext context;

                      // ------ Persistence Context Injection --------

                      @org.jboss.seam.annotations.In("entityManager")
                                  private javax.persistence.EntityManager emanager;
                          public javax.persistence.EntityManager getEntityManager ()
                              {
                              if( emanager==null) // Not injected
                              {
                                  return com.agcs.amd2.model.LocalEntityManager.getCurrentEntityManager();
                              }
                              else
                                  return emanager;
                              }

                    ...
                    }
                  With ejb-jar.xml:
                          <session>
                              <description>
                                  <![CDATA[
                                  The deal between an assured and AGCS. Can be a proposal, a
                  policy or an endorsement.
                                  ]]>
                              </description>
                              <ejb-name>DealDao</ejb-name>
                              <local>com.agcs.amd2.model.deal.DealDao</local>
                              <ejb-class>com.agcs.amd2.model.deal.DealDaoImpl</ejb-class>
                              <session-type>Stateless</session-type>
                              <transaction-type>Container</transaction-type>
                          </session>

                  and components.xml in the business.jar:
                      <component
                          name="dealDao"
                          class="com.agcs.amd2.model.deal.DealDaoImpl"
                          scope="STATELESS"
                          auto-create="true"
                          jndi-name="amanda2/DealDao/local"/>

                  components.xml for the webapp:

                  <?xml version="1.0" encoding="UTF-8"?>
                  <components xmlns="http://jboss.com/products/seam/components"
                              xmlns:core="http://jboss.com/products/seam/core"
                              xmlns:persistence="http://jboss.com/products/seam/persistence"
                              xmlns:transaction="http://jboss.com/products/seam/transaction"
                              xmlns:security="http://jboss.com/products/seam/security"
                              xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                              xmlns:international="http://jboss.com/products/seam/international"
                              xsi:schemaLocation=
                                  "http://jboss.com/products/seam/core http://jboss.com/products/seam/core-2.1.xsd
                                   http://jboss.com/products/seam/persistence http://jboss.com/products/seam/persistence-2.1.xsd
                                   http://jboss.com/products/seam/transaction http://jboss.com/products/seam/transaction-2.1.xsd
                                   http://jboss.com/products/seam/security http://jboss.com/products/seam/security-2.1.xsd
                                   http://jboss.com/products/seam/components http://jboss.com/products/seam/components-2.1.xsd
                                   http://jboss.com/products/seam/international http://jboss.com/products/seam/international-2.1.xsd">

                                   <!---->
                      <core:init debug="false" jndi-pattern="amanda2/#{ejbName}/local" />
                      <persistence:managed-persistence-context name="entityManager" auto-create="true"  persistence-unit-jndi-name="java:/EntityManagerFactories/amanda2" />

                                    <!---->
                                   
                      <!-- international:locale-config default-locale="fr" supported-locales="en fr"/ -->
                      <core:manager conversation-timeout="120000"
                                    concurrent-request-timeout="500"
                                    conversation-id-parameter="cid" default-flush-mode="MANUAL"/>

                      <security:identity authenticate-method="#{loginPage.checkLogin}" />
                     
                          <!-- Support for retrying the page after login -->
                      <event type="org.jboss.seam.security.notLoggedIn">
                          <action execute="#{redirect.captureCurrentView}"/>
                      </event>

                      <event type="org.jboss.seam.security.postAuthenticate">
                          <action execute="#{redirect.returnToCapturedView}"/>
                      </event>
                  <!-- seam-components merge-point -->
                  </components>
                  • 6. Re: convertEntity, lazy loading in view rendering and ejb transaction
                    kapitanpetko

                    Gerard COLLIN wrote on Nov 23, 2009 23:02:


                    In fact, I just made some more testing, and I see that the problem was coming from the Dao and Service bean declaration.
                    I had to remove the javax.ejb.Local and javax.ejb.transaction declarations on my Dao, and I don't know why, because they should be Stateless Session ejb...



                    An abstract class is cannot be a @Local interface, neither can it be an  SLSB. Put the @Local on the interface, the TransactionAttributeType is not needed, it is the default.


                    HTH

                    • 7. Re: convertEntity, lazy loading in view rendering and ejb transaction
                      gcollin.gerardcollin.gmail.com

                      Yes, that was strange Anyway.


                      But as it is generated code, I thought it was ok ...


                      And the bean in ejb-jar.xml is a derived from this class.


                      Now the convertEntity and lazy problem is gone, I have a new one, but it's specific to a page, I will try to solve it by myself and not bother you with that.


                      Thank you again,


                      Regards,


                      G.C.