AS 7 Not inject EntityManager
vitor.eduardods May 2, 2013 2:51 PMHi all.
I know that it is not the first time that someone posts a commentary about this topic. However, see the question bellow and then, think you could help me.
I have an abstract facade like this:
public abstract class AbstractFacade<T, I> { private Class<T> entityClass; public AbstractFacade(Class<T> entityClass) { this.entityClass = entityClass; } public abstract EntityManager getEntityManager(); public void create(T entity) { getEntityManager().persist(entity); } public void edit(T entity) { getEntityManager().merge(entity); } public void remove(T entity) { getEntityManager().remove(getEntityManager().merge(entity)); } public T find(I id) { return getEntityManager().find(entityClass, id); } }
and an implementation like bellow:
@Stateless public class UserBeanFacade extends AbstractFacade<Users, UsersId> { @PersistenceContext(unitName = "ELBapps") private EntityManager manager; public UserBeanFacade() { super(Users.class); } @Override public EntityManager getEntityManager() { return manager; } }
when the line
return getEntityManager().find(entityClass, id);
from abstract facade is executed, generates a NullPointerException:
Caused by: java.lang.NullPointerException at br.com.leaobaio.elbapps.dataaccess.AbstractFacade.find(AbstractFacade.java:38) [elbappsback.jar:]
I have an EJB and a Web modules. My beans.xml is located in META-INF into ejbModule.
I am using: Eclipse Juno; AS 7, JEE 6; EJB 3.1; JPA; and JTA data source.
My data source:
<datasource jta="true" jndi-name="java:/ELBapps" pool-name="ELBapps" enabled="true" use-java-context="true"> <connection-url>jdbc:postgresql:elbapps</connection-url> <driver>org.postgresql</driver> <transaction-isolation>TRANSACTION_READ_COMMITTED</transaction-isolation> <pool> <min-pool-size>10</min-pool-size> <max-pool-size>100</max-pool-size> <prefill>true</prefill> </pool> <security> <user-name>postgres</user-name> <password>postgres</password> </security> <statement> <prepared-statement-cache-size>32</prepared-statement-cache-size> <share-prepared-statements>true</share-prepared-statements> </statement> </datasource>
My persistence.xml
<?xml version="1.0" encoding="UTF-8"?> <persistence version="2.0" xmlns="<a target="_blank" href="http://java.sun.com/xml/ns/persistence"">http://java.sun.com/xml/ns/persistence"</a> xmlns<img src="http://javafree.uol.com.br/forum/images/smiles/icon_mad.gif">si="<a target="_blank" href="http://www.w3.org/2001/XMLSchema-instance"">http://www.w3.org/2001/XMLSchema-instance"</a> xsi:schemaLocation="<a target="_blank" href="http://java.sun.com/xml/ns/persistence">http://java.sun.com/xml/ns/persistence</a> <a target="_blank" href="http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">">http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"></a> <persistence-unit name="ELBapps" transaction-type="JTA"> <provider>org.hibernate.ejb.HibernatePersistence</provider> <jta-data-source>java:/ELBapps</jta-data-source> <class>br.com.leaobaio.elbapps.dataaccess.entities.Profile</class> <class>br.com.leaobaio.elbapps.dataaccess.entities.Resource</class> <class>br.com.leaobaio.elbapps.dataaccess.entities.Article</class> <class>br.com.leaobaio.elbapps.dataaccess.entities.ArticlesStatus</class> <class>br.com.leaobaio.elbapps.dataaccess.entities.Issue</class> <class>br.com.leaobaio.elbapps.dataaccess.entities.Publication</class> <class>br.com.leaobaio.elbapps.dataaccess.entities.Users</class> <properties> <property name="hibernate.hbm2ddl.auto" value="update"/> <property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect"/> <property name="hibernate.archive.autodetection" value="class"/> <property name="hibernate.show_sql" value="true"/> <property name="hibernate.format_sql" value="true"/> </properties> </persistence-unit> </persistence>
Could help me?
Vitor Eduardo