我正在尝试从hibernate 3迁移到JPA2,但我无法获得标准构建器。
我将这些依赖项添加到我的pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.JpaTest</groupId>
<artifactId>JpaTest-parent</artifactId>
<version>1.0-SNAPSHOT</version>
<relativePath>../../pom.xml</relativePath>
</parent>
<artifactId>JpaTest-services</artifactId>
<version>${JpaTest.version}</version>
<packaging>ejb</packaging>
<name>JpaTest-services</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>com.JpaTest</groupId>
<artifactId>JpaTest-modeles</artifactId>
<version>${JpaTest.version}</version>
</dependency>
<!-- EJB 3.0 -->
<dependency>
<groupId>javaee</groupId>
<artifactId>javaee-api</artifactId>
<version>5</version>
<scope>provided</scope>
</dependency>
<!-- JPA 2 -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>4.1.7.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate.java-persistence</groupId>
<artifactId>jpa-api</artifactId>
<version>2.0-cr-1</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-ejb-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
制作了这个EntityManagerProvider类:
package com.JpaTest.utils;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
public class EntityManagerProvider {
private static EntityManagerProvider instance;
private static final String PERSISTENCE_UNIT_NAME = "testPU";
private EntityManagerFactory emf;
private EntityManagerProvider em;
private EntityManagerProvider() {
emf = Persistence.createEntityManagerFactory(PERSISTENCE_UNIT_NAME);
em = (EntityManagerProvider) emf.createEntityManager();
}
public static synchronized EntityManagerProvider getInstance() {
if (instance == null) {
instance = new EntityManagerProvider();
}
return instance;
}
public EntityManagerProvider getEntityManager() {
return em;
}
}
这个DAO:
package com.JpaTest.dao;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import com.JpaTest.model.TestEntity;
public class JpaTestDaoImpl implements IJpaTestDao {
private static final String PERSISTENCE_UNIT_NAME = "testPU";
private EntityManagerFactory emf;
private EntityManager em;
public JpaTestDaoImpl() {
emf = Persistence.createEntityManagerFactory(PERSISTENCE_UNIT_NAME);
em = emf.createEntityManager();
}
@Override
public Integer Create(TestEntity c) {
em.persist(c);
em.flush();
em.refresh(c);
//HibernateUtil.getSessionFactory().getCurrentSession().persist(c);
//HibernateUtil.getSessionFactory().getCurrentSession().flush();
//HibernateUtil.getSessionFactory().getCurrentSession().refresh(c);
return (Integer) c.getId();
}
@Override
public TestEntity Read(Integer id) {
TestEntity c = (TestEntity) em.find(TestEntity.class, id);
//TestEntity c = (TestEntity) HibernateUtil.getSessionFactory().getCurrentSession().get(TestEntity.class, id);
return c;
}
@Override
public List<TestEntity> ReadAll() {
@SuppressWarnings("unchecked")
//List<TestEntity> myList = (List<TestEntity>) HibernateUtil.getSessionFactory().getCurrentSession().createCriteria(TestEntity.class).list();
List<TestEntity> myList = em.get :(
return myList;
}
@Override
public void update(TestEntity c) {
HibernateUtil.getSessionFactory().getCurrentSession().update(c);
HibernateUtil.getSessionFactory().getCurrentSession().flush();
}
@Override
public void delete(TestEntity c) {
HibernateUtil.getSessionFactory().getCurrentSession().delete(c);
HibernateUtil.getSessionFactory().getCurrentSession().flush();
}
}
我看不出我做错了什么。
答案 0 :(得分:0)
里面有一个没有criteriaBuilder的javax.persistence
<groupId>javaee</groupId>
<artifactId>javaee-api</artifactId>
<version>5</version>
转移到
<groupId>org.jboss.javaee</groupId>
<artifactId>jboss-javaee</artifactId>
<version>5.0.1.GA</version>
解决了这个问题。
然而,有一件事我没有得到,它们都是JEE5实现,它们是否应该包含相同的东西?
答案 1 :(得分:0)
您在persistence.xml文件中添加了
<persistence-unit name="servicioPU">
<jta-data-source>java:jboss/datasources/connDS</jta-data-source>
<properties>
<!-- Properties for Hibernate -->
<property name="hibernate.hbm2ddl.auto" value="update" />
<property name="hibernate.show_sql" value="false" />
</properties>
</persistence-unit>