0
votes

" I use Jboss EAP 6.4.7 and the application needs to access datasource configured in Jboss which uses H2 database. The EntityManager is not set in the DAO layer and is returning null. I appreciate any help in correcting or suggesting any idea. Unfortunately the application does not use Spring context and it will be plain JPA-Hibernate model to access the database."

Below is the standalone.xml extract.

<datasource jta="false" jndi-name="java:/ecmConfigDS" pool-name="ecmConfigDS" enabled="true" use-ccm="true">
        <connection-url>jdbc:h2:tcp://localhost/~/ecmconfig</connection-url>
        <driver-class>org.h2.Driver</driver-class>
        <driver>h2</driver>
        <security>
        <user-name></user-name>
        </security>
</data source>    

persistence.xml

<persistence-unit name="ecmFunctionalRoute">
      <provider>org.hibernate.ejb.HibernatePersistence</provider>
      <non-jta-data-source>java:/ecmConfigDS</non-jta-data-source>
      <class>gov.ny.otda.ecm.sharedservices.dao.entity.FunctionRoute</class>
      <properties>
             <property name="hibernate.dialect" value="org.hibernate.dialect.H2Dialect" />
             <property name="hibernate.max_fetch_depth" value="3" />
             <property name="hibernate.hbm2ddl.auto" value="update" />
             <property name="hibernate.show_sql" value="true" />
       </properties>
</persistence-unit>

DAO.java

@PersistenceContext(unitName="ecmFunctionalRoute")
private EntityManager entityManager;
public E findByID(Long ID) throws DAOException {
      return entityManager.find(entityClass, ID);
}
1
provide the class you instantiate your EntityManage please. If it is a static class, your EntityManager cannot be instantiated in that context. - XiCoN JFS

1 Answers

0
votes

Change your persistence.xml like below:

<persistence>
  <persistence-unit name="prod" transaction-type="JTA">
   <provider>org.hibernate.ejb.HibernatePersistence</provider>        
    <jta-data-source>java:/ecmConfigDS</jta-data-source>
    .....
  </persistence-unit>
</persistence>

And check the behavior?

The transaction-type attribute is used to specify whether the entity managers provided by the entity manager factory for the persistence unit must be JTA entity managers or resource-local entity managers. The value of this element is JTA or RESOURCE_LOCAL. A transaction-type of JTA assumes that a JTA data source will be provided—either as specified by the jta-data-source element or provided by the container. In general, in Java EE environments, a transaction-type of RESOURCE_LOCAL assumes that a non-JTA datasource will be provided. In a Java EE environment, if this element is not specified, the default is JTA. In a Java SE environment, if this element is not specified, the default is RESOURCE_LOCAL."