带有Derby的JPA,EntityManager的无持久性提供程序,名为

时间:2015-10-24 18:35:27

标签: maven jpa netbeans derby

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

Project structure

我的实体类:

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 + " ]";
}
}

泽西岛的服务类(仅用于测试目的,我知道使用GET-Request不建议这样做):

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资源时,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)
...

感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

尝试使用

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

由于您正在使用JTA,这就是您通常所做的事情。