Pergunta

I have an EAR file which defines EJB-JAR, WAR and bunch of other JAR files. I have an ejb-jar-1.jar which defines EJBs, Services and Entities. It also defines persistence.xml in the META-INF folder. In the app-war.war I define components.xml which declares persistence:entity-manager-factor and persistence:mananged-persistence-context elements. Now, in one of my services in the ejb-jar-1.jar uses @PersistenceContext annotation for injecting an EntityManager. But when the code is run the EntityManager in that service is always NULL. I'm not sure what's happening. And it is only the EntityManager that it is not able to inject all other injections work properly. Project structure is below.

Any suggestions on how to resolve this issue? Pointers would be helpful as well. I'm upgrading project to run on JBOSS 7.1.1.Final.

Deployement Info

* JBOSS 7.1.1.FINAL
* Uses seam framework: jboss-seam-2.2.1.CR2.jar
* Uses Hibernate 3.4.0.GA

main-ear.ear

|__META-INF
|   |____jboss-deployment-structure.xml
|   |____application.xml
|   |____jboss-app.xml
|   |____MANIFEST.MF
|__lib
|   |____somejar.jar
|   |____somejar.jar
|__ejb-jar-1.jar
|   |____META-INF
|        |____persistence.xml
|__ejb-jar-2.jar
|__jboss-seam-2.2.1.CR2.jar
|__commons-lang-2.4.jar
|__commons-codec-1.1.jar
|__jasypt-1.6.jar
|__app-war.war
    |____components.xml

jboss-deployment-structure.xml

<jboss-deployment-structure xmlns="urn:jboss:deployment-structure:1.0">
    <ear-subdeployments-isolated>false</ear-subdeployments-isolated>
    <deployment>
        <exclusions>
            <module name="javax.faces.api" slot="1.2"/>
            <module name="com.sun.jsf-impl" slot="1.2"/>
            <module name="org.hibernate" slot="main"/>
            <module name="org.hibernate.validator" slot="main"/>
            <module name="org.apache.commons.lang"/>
            <module name="org.apache.commons.collections"/>
            <module name="org.apache.commons.codec"/>
            <module name="org.picketlink" slot="main"/>
            <module name="org.picketbox" slot="main"/>
            <module name="org.javassist" slot="main"/>
            <module name="org.dom4j" slot="main"/>
        </exclusions>
        <dependencies>
            <module name="javax.faces.api" slot="1.2" export="true"/>
            <module name="com.sun.jsf-impl" slot="1.2" export="true"/>
            <module name="org.javassist" slot="1" export="true"/>
            <module name="org.apache.commons.logging" export="true"/>
        </dependencies>
    </deployment>
    <sub-deployment name="app-war.war">
        <exclusions>
            <module name="javax.faces.api" slot="main"/>
            <module name="com.sun.jsf-impl" slot="main"/>
        </exclusions>
        <dependencies>
            <module name="javax.faces.api" slot="1.2"/>
            <module name="com.sun.jsf-impl" slot="1.2"/>
            <module name="deployment.main-ear.ear.ejb-jar-1.jar" />
            <module name="deployment.main-ear.ear.ejb-jar-2.jar" />
            <module name="deployment.main-ear.ear.jboss-seam-2.2.1.CR2.jar"/>
        </dependencies>
    </sub-deployment>
    <module name="deployment.main-ear.ear.jasypt-1.6.jar" />
    <module name="deployment.main-ear.ear.commons-codec-1.1.jar" />
    <module name="deployment.main-ear.ear.commons-lang-2.4.jar" />
</jboss-deployment-structure>

application.xml

<?xml version="1.0" encoding="UTF-8"?>
<application xmlns="http://java.sun.com/xml/ns/j2ee"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/javaee/application_6.xsd"
             version="6">
    <description>myapp</description>
    <display-name>myapp</display-name>
    <initialize-in-order>true</initialize-in-order>
    <library-directory>lib</library-directory>
    <module>
        <ejb>ejb-jar-1.jar</ejb>
    </module>
    <module>
        <ejb>ejb-jar-2.jar</ejb>
    </module>
    <module>
        <ejb>jboss-seam-2.2.1.CR2.jar</ejb>
    </module>
    <module>
        <web>
            <web-uri>app-war.war</web-uri>
            <context-root>/app</context-root>
        </web>
    </module>
</application>

components.xml

<?xml version="1.0" encoding="UTF-8"?>
<components ...>
    <core:init debug="@debug@" jndi-pattern="@jndiPattern@" security-enabled="@seamSecurity@" precedence="30" />
    <persistence:entity-manager-factory name="entityManagerFactory" persistence-unit-name="coreUnit" precedence="30"/>
    <persistence:managed-persistence-context name="entityManager" auto-create="true" entity-manager-factory="#{entityManagerFactory}" precedence="30"/>
    <security:persistent-permission-resolver permission-store="#{cacheEnabledJpaPermissionStore}" />
    <transaction:ejb-transaction precedence="30"/>
    ...
</components>
Foi útil?

Solução

After spending three weeks on this problem I finally found the answer. And the answer simply is Container Managed Persistence in JBOSS 7.1.1.Final for the above project structure i.e. EAR and technologies is broken. It may be working for a WAR I'm not sure. But it doesn't work for an EAR based project.

Upgrade to EAP 6.1.0.Final+ and it will work.

The difference in the 2 builds is that persistence.xml is read at the beginning by the container in the EAP 6.1.0 build but 7.1.1.Final never attempts to read persistence.xml and hence never creates an EntityManagerFactory.

Hope this would help others.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top