JPA 与 Derby,没有名为 EntityManager 的持久性提供程序

JPA with Derby, No Persistence Provider for EntityManager named

我尝试设置一个 RESTful JavaEE 应用程序,它使用 JPA 保存它的数据。首先,我使用 Maven 建立了一个新项目并将其导入到 Netbeans 8.0.2。我创建了一个示例 Derby 数据库(没有表)并将我的应用程序部署到 Glassfish4.1。从 Netbeans IDE 到数据库的连接工作正常(jdbc:derby://localhost:1527/simulation_db [在 APP 上])

我的实体class:

package hftl.simulation.entity;

//imports...

@Entity
public class Agent implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)

private int id;

@Column(name = "name")
private String name;

public int getId() {
    return id;
}

public void setId(int id) {
    this.id = id;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

@Override
public int hashCode() {
    int hash = 0;
    hash += (int) id;
    return hash;
}

@Override
public boolean equals(Object object) {
    // TODO: Warning - this method won't work in the case the id fields are not set
    if (!(object instanceof Agent)) {
        return false;
    }
    Agent other = (Agent) object;
    if (this.id != other.id) {
        return false;
    }
    return true;
}

@Override
public String toString() {
    return "hftl.simulation.entity.Agent[ id=" + id + " ]";
}
}

Jersey 的服务 class(仅用于测试目的,我知道不推荐使用 GET 请求...):

package hftl.simulation.service;

//imports...

@Path("agent")
public class AgentService {

@PersistenceUnit 
private EntityManagerFactory emf = Persistence.createEntityManagerFactory("SimulationPU");
EntityManager em = emf.createEntityManager();

@GET
@Produces(MediaType.TEXT_PLAIN)
public String getAllAgentsGET() {

    Agent a1 = new Agent();
    a1.setId(1);
    a1.setName("pi1");
    em.persist(a1);
    em.close();

    return "GET";
}
}

persistence.xml:

<?xml version="1.0" encoding="UTF-8"?>
<persistence>
<persistence-unit name="SimulationPU" transaction-type="JTA">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider> 
<jta-data-source>jdbc/simulation_DS</jta-data-source>
<class>hftl.simulation.entity.Agent</class>
<properties>
    <property name="toplink.platform.class.name" value="oracle.toplink.essentials.platform.database.DB2Platform"/>
</properties>

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/maven-v4_0_0.xsd">

<modelVersion>4.0.0</modelVersion>

<groupId>hftl.simulation</groupId>
<artifactId>simulation_api</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<name>simulation_api</name>

<build>
    <finalName>simulation_api</finalName>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>2.5.1</version>
            <inherited>true</inherited>
            <configuration>
                <source>1.7</source>
                <target>1.7</target>
            </configuration>
        </plugin>
    </plugins>
</build>

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.glassfish.jersey</groupId>
            <artifactId>jersey-bom</artifactId>
            <version>${jersey.version}</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

<dependencies>
    <dependency>
        <groupId>org.glassfish.jersey.containers</groupId>
        <artifactId>jersey-container-servlet-core</artifactId>
        <!-- use the following artifactId if you don't need servlet 2.x compatibility -->
        <!-- artifactId>jersey-container-servlet</artifactId -->
    </dependency>
    <!-- uncomment this to get JSON support
    <dependency>
        <groupId>org.glassfish.jersey.media</groupId>
        <artifactId>jersey-media-moxy</artifactId>
    </dependency>
    -->
    <dependency>
        <groupId>org.eclipse.persistence</groupId>
        <artifactId>eclipselink</artifactId>
        <version>2.5.2</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>org.eclipse.persistence</groupId>
        <artifactId>org.eclipse.persistence.jpa.modelgen.processor</artifactId>
        <version>2.5.2</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>org.apache.derby</groupId>
        <artifactId>derby</artifactId>
        <version>10.10.1.1</version>
    </dependency>
    <dependency>
        <groupId>javax</groupId>
        <artifactId>javaee-web-api</artifactId>
        <version>7.0</version>
        <type>jar</type>
    </dependency>
</dependencies>
<properties>
    <jersey.version>2.22.1</jersey.version>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

我手动添加了 Derby 的依赖项。我怎么知道应该使用哪个版本的 Derby?部署到 Glassfish 是可行的,但我无法弄清楚持久性提供程序有什么问题。当我尝试通过 http://localhost:8080/simulation_api/agent 调用 REST-Resource 时,Glassfish ServerLog 说:

Information:   simulation_api was successfully deployed in 1.832 milliseconds.
Warnung:   The following warnings have been detected: WARNING: Unknown HK2 failure detected:
MultiException stack 1 of 1
javax.persistence.PersistenceException: No Persistence provider for EntityManager named SimulationPU
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:85)
...

感谢您的帮助。

尝试使用

注入 EntityManager 而不是 EntityManagerFactory
@PersistenceContext(unitName = "SimulationPU")
private EntityManager em;

既然您使用的是 JTA,那么这就是您通常的做法。