我在Eclipse中设置项目时遇到问题,该项目使用纯JPA机制,但Hibernate作为实现层。
我有一个简单的程序,其中2个类是通过多对多关系链接的,所有关系都是使用注释定义的。
我想避免使用特定于hibernate的配置,以便尽可能独立于数据库。例如,我希望我的所有配置都在persistence.xml文件中。
每次启动程序时都会出现以下错误:
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
背景: Eclipse Luna JPA 2.1 Hibernate 4.3.5
这是我的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>
我已经在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;
}
}
当我使用以下方法在DAO对象中手动注入EntityManager时,我有上面提到的错误:
private EntityManager entityManager = DBManager.getEntityManager();
编辑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));
}
}
永远不会注入持久性管理器。
当我尝试使用注释自动注入它时,我在查询时有一个空指针异常。
有人可以帮忙吗?
感谢。
答案 0 :(得分:0)
请提供有关如何使用注释注入EntityManager
的代码。您是否尝试过使用@PersistenceContext
注释?
@PersistenceContext private EntityManager entityManager;
使用@Component
,@Resource
(或Java的本机等效注释)在这种情况下不起作用。
答案 1 :(得分:0)
在persistence.xml
我看不到任何实体声明。您应该在此处定义实体类。像<class>com.example.package.TestEntity<class>
这样的东西。
同样在您的DBManager
中,您应指定您的资源和EntityManger。使用DI,例如:
@PersistenceContext(unitName = "Test-Cache-Hibernate")
private EntityManager em;