0

我正在尝试从休眠 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();
    }


}

我看不出我做错了什么。

4

2 回答 2

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>
于 2014-03-20T15:25:59.753 回答
0

里面有一个没有标准构建器的 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 实现,它们不应该都包含相同的东西吗?

于 2012-10-02T04:51:45.480 回答