0

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

wildfly > modules > com > mycompany > myprovider

folder. 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 face the real problem: it seems i cannot use the @PersistenceContext annotation to inject the entity managare into my EntityManager varible:

@PersistenceContext(unitName = "KAS-Mapping") 
private EntityManager entityManager;

this is my persistence.xml

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1"
xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
    <persistence-unit name="KAS-Mapping">
        <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider> 
            <class>my.class.persistence.model.MapGroup</class>
            <class>my.class.persistence.model.MapUser</class>
            <properties>
                <property name="javax.persistence.provider" value="org.hibernate.ejb.HibernatePersistence" />
                <property name="hibernate.dialect" value="org.hibernate.dialect.Oracle10gDialect"/>
                <property name="javax.persistence.provider" value="org.hibernate.jpa.HibernatePersistenceProvider"/>
                <property name="javax.persistence.transactionType" value="JTA"/>
                <property name="javax.persistence.jtaDataSource" value="java:jboss/datasources/MyDS"/>

        </properties>
    </persistence-unit>
</persistence> 

Why i cannot inject the a context into a jar modules? What am i wrong?

Alex
  • 1,515
  • 2
  • 22
  • 44

2 Answers2

0

WildFly/JBoss modules are not a Java EE concept, so it is not reasonable to expect injection of resources, persistence contexts or CDI beans to work across module boundaries.

Did you try meta-inf="import" on your module dependency? See https://docs.jboss.org/author/display/WFLY9/Class+Loading+in+WildFly

Harald Wellmann
  • 12,615
  • 4
  • 41
  • 63
0

Why does your provider depend on your DAO layer? This is a bad pattern that you should really avoid. Now are you also putting your persistence.xml in your modules?

Have you tried using the standard persistence.xml tags rather than properties?

<persistence-unit name="KAS-Mapping" transaction-type="JTA">
    <jta-data-source>jboss/datasources/MyDS</jta-data-source>
    <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider> 

    <class>my.class.persistence.model.MapGroup</class>
    <class>my.class.persistence.model.MapUser</class>
    <properties>
        <property name="javax.persistence.provider" value="org.hibernate.ejb.HibernatePersistence" />
        <property name="hibernate.dialect" value="org.hibernate.dialect.Oracle10gDialect"/>
    </properties>
</persistence-unit>
Tea Curran
  • 2,923
  • 2
  • 18
  • 22
  • Why you think it s a bad pattern? I need the access to a database togheter with other application/application layer. Shouldn t be useless to replicate the same dao for all of them? – Alex Sep 16 '15 at 04:34
  • I guess i would need to know more about what your provider does. The provider shouldn't depend on the dao though, if there is any dependency it should be provider<-dao<-applicaiton only one way. A more typical way of doing this rather than deploying a module is to build your dao as a versioned .jar file, then include that in your different .war files. – Tea Curran Sep 16 '15 at 04:37
  • It s a keycloak provider and it s needed for authentication puropse. Dao is used to synch keycloak properties file with my own users database. The constraint is that keycloak spi can be deployed just as a module inside jboss, so it s treated like a jboss module i.e. a standalone jar with its own isolation scope. Someone told me that jboss modules are not interested by app server context so it impossible to do injection – Alex Sep 16 '15 at 04:44
  • where is your persistence.xml? in the DAO module? You can have the classes in the DAO but the persistence.xml in your individual webapps. the classpath is available as long as your jboss-deployment-structure.xml depends on your module. – Tea Curran Sep 16 '15 at 04:47
  • I should clarify the focuse on modules def: I mean not a war webapp... but a jar deployed into standalone/modules/my/package/myappname/main ...both dao provider are deployed this way as the keycloak ref guide specify – Alex Sep 16 '15 at 04:52
  • You're not going to be able to use persistence.xml or resource injection in a module. You need a web application for these services to be available. – Tea Curran Sep 16 '15 at 06:36
  • Yes it was finally my conclusion. Thanks – Alex Sep 16 '15 at 07:46