EntityManager Injection, Null Pointer Exception. (Sorry...)
repkin Jun 17, 2008 9:45 AMI wanted the entitymanager have been injected by the container. But after a lot of trial, I decided to ask the question.
Here is the situation,
My ear file structure:
surgeonfish.ear |----surgeonfish-ds.xml |----META-INF |----application.xml |----jboss-app.xml |----MANIFEST.MF |----surgeonfish.jar |----com.beans... |----META-INF |----persistence.xml |----MANIFEST.MF |----surgeonfish.war |----META-INF |----MANIFEST.MF |----WEB-INF |----classes |----lib |----web.xml
--------
surgeonfish-ds.xml:
<?xml version="1.0" encoding="UTF-8"?> <datasources> <local-tx-datasource> <jndi-name>time_reporting_data_source</jndi-name> <connection-url>jdbc:mysql://localhost:3306/timereporting</connection-url> <driver-class>com.mysql.jdbc.Driver</driver-class> <user-name>root</user-name> <password>test</password> <exception-sorter-class-name>org.jboss.resource.adapter.jdbc.vendor.MySQLExceptionSorter</exception-sorter-class-name> <metadata> <type-mapping>mySQL</type-mapping> </metadata> </local-tx-datasource> </datasources>
----------------
application.xml
<application> <display-name>Surgeonfish</display-name> <module> <web> <web-uri>surgeonfish.war</web-uri> <context-root>/surgeonfish</context-root> </web> </module> <module> <ejb>surgeonfish.jar</ejb> </module> </application>
---------------
jboss-app.xml:
<jboss-app> <module> <service>surgeonfish-ds.xml</service> </module> <loader-repository> surgeonfish:archive=surgeonfish.ear </loader-repository> </jboss-app>
--------------
persistence.xml:
<persistence> <persistence-unit name="time_report_persistence_context"> <provider>org.hibernate.ejb.HibernatePersistence</provider> <jta-data-source>java:/time_reporting_data_source</jta-data-source> <jar-file>../surgeonfish.jar</jar-file> <properties> <property name="hibernate.hbm2ddl.auto" value="create-drop"/> <property name="hibernate.show_sql" value="true"/> <property name="dialect">org.hibernate.dialect.MySQLDialect</property> <property name="hibernate.transaction.manager_lookup_class" value="org.hibernate.transaction.JBossTransactionManagerLookup"/> </properties> </persistence-unit> </persistence>
----------------------
Here my test bean:
package com.xyz...; import javax.ejb.Local; import javax.ejb.Stateless; import javax.persistence.EntityManager; import javax.persistence.EntityManagerFactory; import javax.persistence.Persistence; import javax.persistence.PersistenceContext; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; @Stateless @Local(value=PrStaffInterface.class) public class PrStaffHome { private static final Log log = LogFactory.getLog(PrStaffHome.class); @PersistenceContext(unitName="time_report_persistence_context") private EntityManager entityManager; public PrStaff findById(Integer id) { log.debug("getting PrStaff instance with id: " + id); try { /* EntityManagerFactory emf = Persistence.createEntityManagerFactory("time_report_persistence_context"); EntityManager em = emf.createEntityManager(); PrStaff instance1 = em.find(PrStaff.class, id); ---this lines working without any error...--- */ PrStaff instance = entityManager.find(PrStaff.class, id);->I am taking null pointer exception here... System.out.println("instance:"+instance); log.debug("get successful"); return instance; } catch (RuntimeException re) { log.error("get failed", re); throw re; } } }
I cant find reason, why container is not injecting the entity manager. Of course I am missing something but I cant find. Thanks for your helps...