5 Replies Latest reply on May 2, 2016 2:07 PM by lovem

    EntityManager is not injected inside a Jboss module

    aliosha79

      I'm working with jboss wildfly 9. I have a provider deployed as module into the   modules  directory.

      Then i have a jpa project with DAO pattern writing and reading inside my database. I want to handle the DAO transaction using JTA but in order to make the DAO class visibile to myprovider i need to put the DAO JPA project inside the modules directory too.

      Now i m facing  the real problem: it seems i cannot use the PersistenceContext annotation to inject the entity manager into my EntityManager variable wich is always null. Why i cannot inject the a context into a jar modules? What am i wrong?

        • 1. Re: EntityManager is not injected inside a Jboss module
          smarlow

          You can enable TRACE logging for org.jboss.as.jpa which might give you a hint as to what is not working.  Instructions for enable TRACE logging are here@.

          • 2. Re: EntityManager is not injected inside a Jboss module
            aliosha79

            Thanks for the asnwer.

            Following your link i entered the standalone.xml file and i have activated the trace log leve.

            During the wildfly startup i see trace log lines like :

            TRACE [org.jboss.as.jpa] (MSC service thread 1-1) parse checking if

            "/D:/CodeHome/keycloak-demo-1.4.0.Final/keycloak/bin/content/MyFistModule.jar/META-INF/persistence.xml" exists, result = false

            and

            TRACE [org.jboss.as.jpa] (MSC service thread 1-1) parsed persistence unit definitions for jar MyFirstModule.jar

            MyFirstModule is a keycloak provider and is deployedby the web admin console deployments procedure.

             

             

            But i don't see any reference to MySecondModule which is the one implementing the JPA and DAO logic.


             

             

            I have deployed it simply by creating : <Wildfly-server>/modules/com/myapplication/MySecondModule/main/MySecondModule.jar

            and creating the module.xml

             

            <?xml version="1.0" ?>

            <module xmlns="urn:jboss:module:1.1" name="com.myapplication.MySecondModule">

                <resources>

                    <resource-root path="MySecondModule.jar .jar"/>

                </resources>

                <dependencies>

                    <module name="org.hibernate"/>

                   <module name="org.apache.commons.logging" />

                   <module name="javax.api"/>

                   <module name="javax.persistence.api"/>

                   <module name="org.javassist"/>

                   </dependencies>

            </module>

             

            Do i miss anything?

            • 3. Re: EntityManager is not injected inside a Jboss module
              smarlow

              MySecondModule is not going to be deployed like an application would be.  Application deployments are scanned for persistence.xml files but <wildfly>modules are not.  So the persistence context will always be null, unless you handle initializing it yourself.

               

              For example, you could initialize it via javax.persistence.Persistence.createEntityManagerFactory(String puName, Map properties), however, the (persistence unit specificed) providerName class has to be available when starting the application server.  Keep the org.jboss.as.jpa TRACE logging enabled, while you set this up.  I think the TRACE log output will help.  Depending on which persistence provider implementation you are using, you might also enable TRACE logging for that (e.g. org.hibernate).

               

              With a change to use the Persistence bootstrap, you are going to manage the EntityManagerFactory directly.  Depending on which persistence provider you are using, you may also need to pass certain integration property settings on the call to createEntityManagerFactory.  Keep in mind that EntityManagerFactories are thread safe but EntityManager's are not.

               

              Scott

              • 4. Re: EntityManager is not injected inside a Jboss module
                aliosha79

                really thanks for the answer. Indeed using a Java SE approach is what i did till now, even if i was not surrending to the idea of not having container handled transactions.

                So i will proceed with Persistence.createEntityManagerFactory method.

                Thanks

                • 5. Re: EntityManager is not injected inside a Jboss module
                  lovem

                  Hi Alessio,

                   

                  Did you manage to resolve this issue as I have a similar problem with a module

                   

                  Thanks