I'm trying to inject EntityManagerFactory using @PersistenceUnit, but it's always null.
I think my persistence.xml is OK, since I can get the EntityManager with this code:
EntityManager em = Persistence.createEntityManagerFactory("myPersistenceUnit").createEntityManager();
So, I would like to know if I'm doing something wrong, or if this is not possible when using Jersey (2.23) and Wildfly 10 (JBoss EAP 7).
Here is what I've done so far:
- Created a jersey-quickstart-webapp maven project on eclipse;
Added the following dependencies to my pom.xml:
<dependency> <groupId>javax</groupId> <artifactId>javaee-api</artifactId> <version>7.0</version> </dependency> <dependency> <groupId>org.hibernate.javax.persistence</groupId> <artifactId>hibernate-jpa-2.1-api</artifactId> <version>1.0.0.Final</version> </dependency> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-entitymanager</artifactId> <version>5.2.2.Final</version> </dependency> <dependency> <groupId>com.hynnet</groupId> <artifactId>oracle-driver-ojdbc6</artifactId> <version>12.1.0.1</version> </dependency>
Created the persistence.xml:
<persistence-unit name="myPersistenceUnit" transaction-type="RESOURCE_LOCAL"> <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider> <!-- All persistence classes must be listed --> <class>com.mps.classes.TermosPesquisados</class> <properties> <!-- Provider-specific connection properties --> <property name="javax.persistence.jdbc.driver" value="oracle.jdbc.OracleDriver" /> <property name="javax.persistence.jdbc.url" value="JDBC_URL" /> <property name="javax.persistence.jdbc.user" value="USER" /> <property name="javax.persistence.jdbc.password" value="PASSWORD" /> <property name="hibernate.dialect" value="org.hibernate.dialect.Oracle10gDialect" /> <property name="hibernate.show_sql" value="false" /> <property name="hibernate.connection.release_mode" value="after_transaction" /> <property name="hibernate.connection.isolation" value="2" /> </properties> </persistence-unit>
Modified the MyResource.java:
@ManagedBean @Path("myresource") public class MyResource { @PersistenceUnit(unitName= "myPersistenceUnit") private EntityManagerFactory emf; @GET @Produces(MediaType.TEXT_PLAIN) public String getIt() { if(emf == null) return "emf is null"; return "emf is not null"; } }
Added an empty beans.xml (not sure if it's necessary);