2015-10-24 112 views
0

我嘗試設置一個RESTful JavaEE-應用程序,它使用JPA保存它的數據。首先,我使用Maven創建了一個新項目並將其導入到Netbeans 8.0.2。我創建了一個示例Derby數據庫(沒有表),並將我的應用程序部署到Glassfish4.1。從NetBeans IDE中對數據庫的連接工作正常(JDBC:德比://本地主機:1527/simulation_db [上APP])JPA與德比,沒有持久性提供者的EntityManager名爲

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

的服務類澤西島(只是出於測試目的,我知道這不是recommandable對這一帶有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?部署到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) 
... 

感謝您的幫助。

回答

0

嘗試用

@PersistenceContext(unitName = "SimulationPU") 
private EntityManager em; 

注入的EntityManager而不是EntityManagerFactory的。由於你使用JTA,那你是怎麼會通常做。

相關問題