title: how to setup JPA project to work with Hibernate?
body: <p>I'm having trouble setting up a project in Eclipse that uses pure JPA mechanisms but with Hibernate as the implementation layer.</p>
  
  <p>I have a simple program with 2 classes linked by a many-to-many relationship all defined using annotations.</p>
  
  <p>I would like to avoid using hibernate specific configuration in order to stay as independent as possible from the database. For instance I'd like all my configuration to be in the persistence.xml file.</p>
  
  <p>Each time I start the program I have the following error :</p>
  
  <pre><code>juil. 18, 2014 7:28:11 PM org.hibernate.ejb.HibernatePersistence logDeprecation
  WARN: HHH015016: Encountered a deprecated javax.persistence.spi.PersistenceProvider [org.hibernate.ejb.HibernatePersistence]; use [org.hibernate.jpa.HibernatePersistenceProvider] instead.
  juil. 18, 2014 7:28:11 PM org.hibernate.ejb.HibernatePersistence logDeprecation
  WARN: HHH015016: Encountered a deprecated javax.persistence.spi.PersistenceProvider [org.hibernate.ejb.HibernatePersistence]; use [org.hibernate.jpa.HibernatePersistenceProvider] instead.
  juil. 18, 2014 7:28:11 PM org.hibernate.ejb.HibernatePersistence logDeprecation
  WARN: HHH015016: Encountered a deprecated javax.persistence.spi.PersistenceProvider [org.hibernate.ejb.HibernatePersistence]; use [org.hibernate.jpa.HibernatePersistenceProvider] instead.
  juil. 18, 2014 7:28:11 PM org.hibernate.jpa.internal.util.LogHelper logPersistenceUnitInformation
  INFO: HHH000204: Processing PersistenceUnitInfo [
      name: Test-Cache-Hibernate
      ...]
  juil. 18, 2014 7:28:11 PM org.hibernate.Version logVersion
  INFO: HHH000412: Hibernate Core {4.3.5.Final}
  juil. 18, 2014 7:28:11 PM org.hibernate.cfg.Environment &lt;clinit&gt;
  INFO: HHH000206: hibernate.properties not found
  juil. 18, 2014 7:28:11 PM org.hibernate.cfg.Environment buildBytecodeProvider
  INFO: HHH000021: Bytecode provider name : javassist
  juil. 18, 2014 7:28:11 PM org.hibernate.annotations.common.reflection.java.JavaReflectionManager &lt;clinit&gt;
  INFO: HCANN000001: Hibernate Commons Annotations {4.0.4.Final}
  Initial SessionFactory creation failed.javax.persistence.PersistenceException: Unable to build entity manager factory
  </code></pre>
  
  <p>context :
   Eclipse Luna
   JPA 2.1
   Hibernate 4.3.5</p>
  
  <p>here is my persistence.xml :</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd"&gt;
      &lt;persistence-unit name="Test-Cache-Hibernate" transaction-type="RESOURCE_LOCAL"&gt;
          &lt;provider&gt;org.hibernate.jpa.HibernatePersistenceProvider&lt;/provider&gt;
          &lt;non-jta-data-source&gt;datasource&lt;/non-jta-data-source&gt;
          &lt;shared-cache-mode&gt;ENABLE_SELECTIVE&lt;/shared-cache-mode&gt;
          &lt;properties&gt;
              &lt;property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/testcache"/&gt;
              &lt;property name="javax.persistence.jdbc.user" value="root"/&gt;
              &lt;property name="javax.persistence.jdbc.password" value="admin"/&gt;
              &lt;property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/&gt;
          &lt;/properties&gt;
      &lt;/persistence-unit&gt;
  &lt;/persistence&gt;
  </code></pre>
  
  <p>I have isolated the creation of the entity manager in a Singleton class that is as follows :</p>
  
  <pre><code>package src.persistence.dbmanager;
  
  import javax.persistence.EntityManager;
  import javax.persistence.Persistence;
  
  public class DBManager {
  
      public static final EntityManager entityManager;
  
      static {
          try {
              entityManager = Persistence.createEntityManagerFactory("Test-Cache-Hibernate").createEntityManager();
          } catch (Throwable ex) {
          throw new ExceptionInInitializerError(ex);
          }
      }
  
      public static EntityManager getEntityManager() {
          return entityManager;
      }
  }
  </code></pre>
  
  <p>I have the error mentionned above when I manually inject the EntityManager in the DAO objects using :</p>
  
  <pre><code>private EntityManager entityManager = DBManager.getEntityManager();
  </code></pre>
  
  <p>Edit 1 :
  Here the code of one of my DAO object :</p>
  
  <pre><code>package acs.testcache.persistence.dao;
  
  import java.util.HashSet;
  import java.util.List;
  
  import javax.persistence.EntityManager;
  import javax.persistence.PersistenceContext;
  import javax.persistence.Query;
  
  import acs.testcache.persistence.entities.MyChildObject;
  import acs.testcache.persistence.entities.MyParentObject;
  
  
  public class MyChildDAO extends MyCommonDAO&lt;MyChildObject&gt; {
  
      @PersistenceContext(unitName="Test-Cache-Hibernate")
      private EntityManager entityManager;
  
      public MyChildDAO() {
          super(MyChildObject.class);
      }
  
      public EntityManager getEntityManager() {
          return entityManager;
      }
  
      @SuppressWarnings("unchecked")
      public void getParents(MyChildObject c){
  
          StringBuilder hqlQuery = new StringBuilder();
          hqlQuery.append("select c.parentsObjects from MyChildObject c where c.id = :id");
  
          Query q = getEntityManager().createQuery(hqlQuery.toString()).setParameter("id", c.getId()).setHint("org.hibernate.cacheable", true);
          List&lt;MyParentObject&gt; result = q.getResultList();
  
          c.setParents(new HashSet&lt;MyParentObject&gt;(result));
      }
  }
  </code></pre>
  
  <p>The persistence manager is never injected.</p>
  
  <p>when I try using the annotation to automatically inject it I have a null pointer exception when querying.</p>
  
  <p>Can anybody help please ?</p>
  
  <p>Thanks.</p>