Reputation: 5528
I'm using Wildfly 9.0.1 Final with a working JNDI data source. I've set up all my entities, but I can't seem to get it working. I'm trying to inject the EntityManager using the PersistenceContext, but it doesn't seem to be working, and it's throwing a null pointer exception:
persistence.xml
<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence" version="2.0">
<persistence-unit name="pu" transaction-type="JTA">
<jta-data-source>java:jboss/jdbc/ds</jta-data-source>
<class>EventEntity</class>
<class>EventDaoImpl</class>
<properties>
<property name="hibernate.show_sql" value="true"/>
<property name="hibernate.format_sql" value="true"/>
<property name="hbm2ddl.auto" value="update"/>
<property name="hibernate.archive.autodetection" value="class"/>
</properties>
</persistence-unit>
</persistence>
EventEntity is the Entity, and EventDaoImpl is the class which handles basic CRUD operations on it:
@Stateless
public class EventDaoImpl {
@PersistenceContext(unitName = "pu")
private EntityManager em;
public List<EventEntity> getEvents() {
Query q = em.createQuery("SELECT e from EventEntity AS e");
return q.getResultList();
}
}
It may be worth mentioning that the JPA and the DaoImpl are in one maven module, while I'm trying to access it from another module. The dependencies are correct. I'm testing this in another class with the following code:
EventDaoImpl edi = new EventDaoImpl();
List<EventEntity> events = edi.getEvents();
I tried specifying the provider the the persistence.xml file, like this:
<provider>org.hibernate.ejb.HibernatePersistence</provider>
but my IDE says it can't resolve anything past the org.hibernate
, which is strange, since I do have a maven dependency set on hibernate in the jpa pom.xml:
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>4.3.10.Final</version>
</dependency>
Any help would be appreciated.
Upvotes: 2
Views: 2605
Reputation: 167
EventDaoImpl edi = new EventDaoImpl();
should be:
@Inject
EventDaoImpl edi;
or:
@EJB
EventDaoImpl edi;
This is becuase you have annotated the EventDaoImpl as a stateless bean. So you have to inject it as a bean
Upvotes: 4