3 Replies Latest reply on Jun 24, 2013 12:28 AM by sfcoy

    EntityManager is null. @PersistenceContext not working

    j_pramanik

      Hi Folks,

       

      I'm new in EJB3 and Hibernate. I was creating one application using EJB3 and Hibernate but due to EntityManager is always null. Therefore can not make any transaction. Can you please help me ?

       

      My persistence.xml file looks like below -

       

      <?xml version="1.0" encoding="UTF-8"?>

      <persistence xmlns="http://java.sun.com/xml/ns/persistence"

                xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

                xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"

                version="2.0">

       

                <persistence-unit name="JSFTestPU" transaction-type="JTA">

             

              <provider>org.hibernate.ejb.HibernatePersistence</provider>

                          <jta-data-source>java:jboss/datasources/PostgresDS</jta-data-source>

                          <properties>

                                    <property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect" />

                              <property name="hibernate.show_sql" value="true" />

                                    <property name="hibernate.format_sql" value="true" />

                                    <property name="hibernate.max_fetch_depth" value="3" />

                                    <property name="hibernate.default_schema" value="postgres" />

                                    <property name="hibernate.jdbc.batch_size" value="650" />

                                    <property name="hibernate.connection.driver_class" value="org.postgresql.Driver" />

                                    <property name="hibernate.connection.url" value="jdbc:postgresql://localhost:5432/test" />

                                    <property name="hibernate.connection.username" value="postgres" />

                                    <property name="hibernate.connection.password" value="pgadmin" />

                                    <property name="hibernate.connection.autocommit" value="true" />

       

                          </properties>

             

                </persistence-unit>

       

      </persistence>

       

       

      I'm using JBOSS AS 7.1.1 application server and Database is Postgresql DB.

       

      Standalone.xml configuration file looks like -

       

      <datasource jta="true" jndi-name="java:jboss/datasources/PostgresDS" pool-name="PostgresDS" enabled="true">

                          <connection-url>jdbc:postgresql://localhost:5432/test</connection-url>

                          <driver-class>org.postgresql.Driver</driver-class>

                          <driver>postgresql-jdbc4</driver>

                          <pool>

                              <min-pool-size>5</min-pool-size>

                              <max-pool-size>20</max-pool-size>

                              <prefill>true</prefill>

                          </pool>

                          <security>

                              <user-name>postgres</user-name>

                              <password>********</password>

                          </security>

      </datasource>

       

      <drivers>

             <driver name="postgresql-jdbc4" module="com.pgserver.jdbc"/>

      </drivers>

       

      My DAO class is like below which an abstract class extended in other DAO and used -

       

      abstract class GenericDAO<T> implements Serializable {

      private static final long serialVersionUID = 1L;

       

      @PersistenceContext(unitName="JSFTestPU")

      private EntityManager em;                        ///// Here EntityManager is always returned as NULL

       

       

      private Class<T> entityClass;

       

       

      public void beginTransaction() {

                 try{              

              em.getTransaction().begin();

                 }catch(Exception e1){

                           System.out.println(e1.getMessage());

                           e1.printStackTrace();

                           System.out.println("Entity Manager :"+em);

                }  

       

      }

       

       

      public void commit() {

        em.getTransaction().commit();

      }

       

       

      public void rollback() {

        em.getTransaction().rollback();

      }

       

       

      public void closeTransaction() {

        em.close();

      }

       

       

      public void commitAndCloseTransaction() {

        commit();

        closeTransaction();

      }

       

       

      public void flush() {

        em.flush();

      }

       

       

      public void joinTransaction() { 

          em.joinTransaction();

      }

       

       

      public GenericDAO(Class<T> entityClass) {

        this.entityClass = entityClass;

      }

       

       

      public T save(T entity) {

        try {

                  beginTransaction();

                 

                  em.persist(entity);

                  commitAndCloseTransaction();

      } catch (Exception e) {    

                e.printStackTrace();

      }

        return entity;

      }

       

      ....

       

      ....

       

      }

       

      The problem is EntityManager is always returned as NULL. Can you anybody help me to resolve this problem ?????

       

      Thanks in advance.

       

      Jayanta P.