简体   繁体   中英

how to setup JPA project to work with Hibernate?

I'm having trouble setting up a project in Eclipse that uses pure JPA mechanisms but with Hibernate as the implementation layer.

I have a simple program with 2 classes linked by a many-to-many relationship all defined using annotations.

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.

Each time I start the program I have the following error :

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 <clinit>
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 <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {4.0.4.Final}
Initial SessionFactory creation failed.javax.persistence.PersistenceException: Unable to build entity manager factory

context : Eclipse Luna JPA 2.1 Hibernate 4.3.5

here is my persistence.xml :

<?xml version="1.0" encoding="UTF-8"?>
<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">
    <persistence-unit name="Test-Cache-Hibernate" transaction-type="RESOURCE_LOCAL">
        <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
        <non-jta-data-source>datasource</non-jta-data-source>
        <shared-cache-mode>ENABLE_SELECTIVE</shared-cache-mode>
        <properties>
            <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/testcache"/>
            <property name="javax.persistence.jdbc.user" value="root"/>
            <property name="javax.persistence.jdbc.password" value="admin"/>
            <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
        </properties>
    </persistence-unit>
</persistence>

I have isolated the creation of the entity manager in a Singleton class that is as follows :

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;
    }
}

I have the error mentionned above when I manually inject the EntityManager in the DAO objects using :

private EntityManager entityManager = DBManager.getEntityManager();

Edit 1 : Here the code of one of my DAO object :

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<MyChildObject> {

    @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<MyParentObject> result = q.getResultList();

        c.setParents(new HashSet<MyParentObject>(result));
    }
}

The persistence manager is never injected.

when I try using the annotation to automatically inject it I have a null pointer exception when querying.

Can anybody help please ?

Thanks.

Please provide the code for how you inject the EntityManager using annotations. Have you tried using @PersistenceContext annotation like so?

@PersistenceContext private EntityManager entityManager;

Using @Component , @Resource (or whatever Java's native equivalent annotation is) will not work in this case.

In persistence.xml I cannot see any declaration of entities. You should define entity classes here. Something like <class>com.example.package.TestEntity<class> .

Also in your DBManager you should specify what are your resources and EntityManger. Use DI, something like: @PersistenceContext(unitName = "Test-Cache-Hibernate")
private EntityManager em;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM