简体   繁体   English

如何设置JPA项目以与Hibernate一起使用?

[英]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. 我在使用纯JPA机制但将Hibernate作为实现层的Eclipse中设置项目时遇到了麻烦。

I have a simple program with 2 classes linked by a many-to-many relationship all defined using annotations. 我有一个简单的程序,其中包含2个类,这些类通过使用注释定义的多对多关系链接。

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. 例如,我希望所有配置都在persistence.xml文件中。

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 上下文:Eclipse Luna JPA 2.1 Hibernate 4.3.5

here is my persistence.xml : 这是我的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 : 我在Singleton类中隔离了实体管理器的创建,如下所示:

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 : 我在使用DAO对象手动注入EntityManager时遇到上述错误:

private EntityManager entityManager = DBManager.getEntityManager();

Edit 1 : Here the code of one of my DAO object : 编辑1:这是我的DAO对象之一的代码:

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. 请提供有关如何使用注释注入EntityManager的代码。 Have you tried using @PersistenceContext annotation like so? 您是否尝试过像这样使用@PersistenceContext注释?

@PersistenceContext private EntityManager entityManager; @PersistenceContext私有EntityManager实体管理器;

Using @Component , @Resource (or whatever Java's native equivalent annotation is) will not work in this case. 在这种情况下,使用@Component@Component @Resource (或任何Java的本机等效注解)将不起作用。

In persistence.xml I cannot see any declaration of entities. persistence.xml我看不到任何实体声明。 You should define entity classes here. 您应该在此处定义实体类。 Something like <class>com.example.package.TestEntity<class> . 类似于<class>com.example.package.TestEntity<class>

Also in your DBManager you should specify what are your resources and EntityManger. 同样在DBManager您应该指定什么是资源和EntityManger。 Use DI, something like: @PersistenceContext(unitName = "Test-Cache-Hibernate") 使用DI,例如: @PersistenceContext(unitName = "Test-Cache-Hibernate")
private EntityManager em;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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