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);
There is no point for a @ManagedBean annotation here, this is a JSF annotation and I according to your code you're trying to expose a REST layer.
Just remove it and all will be fine (also ensure that you have a
beans.xml
in your classpath to enable CDI, otherwise annotate your class with @Stateless)I think Entity manager should be enough :
It seems that Jersey conflicts with Resteasy. This way, I had 2 options:
I ended up choosing the 2nd option because it was easier and I have no reasons to use specifically Jersey.
This way, I had to change my web.xml, replacing this:
For this:
*Another option was to create a class extending Application class.
*beans.xml was not necessary.
Then, I annotated my resource class with @Stateless and I was able to inject EntityManager properly:
At this point, EntityManager was OK, but somehow it was using JBoss h2 in-memmory database (ExampleDS). So, I configured an oracle datasource on JBoss (OracleDS) and updated my persistence.xml to use OracleDS and JTA instead of "RESOURCE_LOCAL":
With those steps I was able to inject EntityManager and make my CRUD operations successfully.