2 Replies Latest reply on Aug 29, 2011 2:50 PM by gebuh

    injecting entityManager into a POJO

    gebuh

      Hi all, I'm using Seam 2.2.0 with jboss 5.1.0GA. 
      I'm trying to inject an entityManager into a POJO action bean:




      @In
      EntityManager entityManager;




      I can get an entityManager with


      (EntityManager)Component.getInstance("entityManager")



      But then I get LIE errors for entity beans unless I make all FetchTypes eager. 
      I've tried all manner of configuration changes, but nothing is working, if I use an injected entityManager it's always null.  Is it even possible?
      My current persistence.xml:



      <persistence-unit name="myApplicationName" transaction-type="JTA">
            <provider>org.hibernate.ejb.HibernatePersistence</provider>
            <jta-data-source>java:/MyDatasource</jta-data-source>
            <properties>
               <property name="hibernate.dialect" value="org.hibernate.dialect.OracleDialect"/>
               <property name="hibernate.hbm2ddl.auto" value="validate"/>
               <property name="hibernate.show_sql" value="true"/>
               <property name="hibernate.format_sql" value="true"/> 
               <property name="hibernate.default_schema" value="MY_PROD"/>
               <!-- Only relevant if Seam is loading the persistence unit (Java SE bootstrap) -->
               <property name="hibernate.transaction.manager_lookup_class" value="org.hibernate.transaction.JBossTransactionManagerLookup"/>
               <!-- for session logging -->
               <property name="jboss.entity.manager.factory.jndi.name" value="java:/EntityManagers/MyEMFactory"/>
               
            </properties>
         </persistence-unit>


      current components.xml:



       <persistence:entity-manager-factory name="entityManagerFactory" persistence-unit-name="myApplicationName"/>
      <persistence:managed-persistence-context name="entityManager" auto-create="true"
                            persistence-unit-jndi-name="java:/EntityManagers/MyEMFactory"/>



      I've tried:



      <factory name="hibernateSession" value="#{entityManager.delegate}" scope="stateless" auto-create="true"/>
           <persistence:managed-persistence-context name="entityManager" auto-create="true"
                entity-manager-factory="#{entityManagerFactory}"/>



      and



      <persistence:hibernate-session-factory name="hibernateSessionFactory"/>
           <persistence:managed-hibernate-session name="hibernateSession" session-factory="#{hibernateSessionFactory}"/>



      I've tried the Seam documentation and examples, but nothing I'm doing is working, I'd appreciate any advice.

        • 1. Re: injecting entityManager into a POJO
          gebuh

          Bump for rescue.  There's configuration information for Jboss 5 using EJB's, Jboss 4 without.  I have an exploded WAR using Jboss 5 and this will not work.  If I add an EMF the app won't deploy, if I reference a jndi name in persistence.xml the app won't load.  I tried removing the persistence-unit-ref-name reference from web.xml, added and removed <transaction:entity-transaction /> <transaction:ejb-transaction /> tags from components.xml.  Used the default values and hardcoded them one at a time.
          I still can't get this to work.

          • 2. Re: injecting entityManager into a POJO
            gebuh

            I'm currently trying this configuration:


            persistence.xml:




            <persistence-unit name="myEmployee" transaction-type="JTA">
                  <provider>org.hibernate.ejb.HibernatePersistence</provider>
                  <jta-data-source>java:/myEmployeeDatasource</jta-data-source>
                  <properties>
                     <property name="hibernate.dialect" value="org.hibernate.dialect.OracleDialect"/>
                     <property name="hibernate.hbm2ddl.auto" value="validate"/>
                     <property name="hibernate.show_sql" value="true"/>
                     <property name="hibernate.format_sql" value="true"/> 
                     <property name="hibernate.default_schema" value="MY_PROD"/>
                     <!-- Only relevant if Seam is loading the persistence unit (Java SE bootstrap) -->
                     <property name="hibernate.transaction.manager_lookup_class" value="org.hibernate.transaction.JBossTransactionManagerLookup"/>
                  </properties>
               </persistence-unit>




            web.xml:


            <persistence-unit-ref>
              <persistence-unit-ref-name>myEmployee/pu</persistence-unit-ref-name>
              <persistence-unit-name>myEmployee</persistence-unit-name>
             </persistence-unit-ref>




            components.xml:



            <persistence:managed-persistence-context name="entityManager"
            persistence-unit-jndi-name="java:comp/env/myEmployee/pu"
            auto-create="true"/>




            This deploys and starts correctly, but entityManager is still null.
            When I add this to components.xml:



            <transaction:entity-transaction entity-manager="#{entityManager}"/>



            The app deploys correctly, but any attempt to access the application results in




            firefox has detected that the server is redirecting the request for this address in a way that will never complete

            errors in my browser.



            I have a bunch of EntityHome classes in the same package, these can use an entityManager with no problem.  All the docs I've looked at state that I should be able to inject an entityManager, is there some other initialization I need to do to be able to inject it into a JavaBean?
            Somebody please throw me a bone.