2015-10-24 3 views
0

Я пытаюсь настроить RESTful JavaEE-приложение, которое сохраняет его данные с помощью JPA. Во-первых, я создал новый проект с Maven и импортировал его в Netbeans 8.0.2. Я создал образец базы данных Derby (без таблиц) и развернул мое приложение в Glassfish4.1. Соединение с Netbeans IDE к базе данных работает отлично (JDBC: Derby: // LOCALHOST: 1527/simulation_db [на APP])JPA с Derby, No Persistence Provider для 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 + " ]"; 
} 
} 

Класс обслуживания для Джерси (только для целей тестирования, я знаю, что это не рекомендуем к этому с 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 работает, но я не могу понять, в чем проблема с поставщиком непрерывности. Когда я пытаюсь вызова REST-ресурс с помощью http://localhost:8080/simulation_api/agent 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

Попробуйте инъекции EntityManager вместо EntityManagerFactory с

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

Поскольку вы используете JTA, то есть, как вы обычно делаете.

Смежные вопросы