WildFlyとMavenを使用して単純なEJBのサンプルを実行しようとしていましたが、永続性を作成するはずのステージ単位。 私はPostgreSQLデータベースを作成し、Wildflyのデータソースを設定し、persistence.xmlファイルを作成しました。しかし、それは、WARのように思えるがpersistence.xmlのが含まれたことがないので、毎回私はWildflyから、次の情報およびエラーを取得EntityManagerを作成しよう:クラスパス(wildfly、maven)にMETA-INF/persistence.xmlファイルが見つかりません
11:55:57664 INFO [org.hibernate.jpa.boot。 HHH000318:クラスパスにMETA-INF/persistence.xmlファイルが見つかりませんでした。
11:55:57,666 ERROR [io.undertow.request](デフォルトのタスク-1) UT005023:/ StudentInfoAppServer/rest/tutorial/helloworldに対する例外処理リクエスト:org.jboss.resteasy.spi.UnhandledException:javax.persistence.PersistenceException:PostgresというEntityManagerの永続プロバイダがありません
私のpersistence.xmlが(私は持続性ユニットを作成したときにアイデアがそれを置く)のsrc/META-INFフォルダの下に、現在ですが、私も試してみた:
- のsrc /メイン/ javaの/ META-INF
- のsrc /メイン/ webappの/ WEB-INF /クラス/ META-INF
- のsrc /メイン/リソース/ META-INF
persistence.xmlの
<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence" version="2.0">
<persistence-unit name="postgres">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<class>com.dain_torson.appserver.Person</class>
<properties>
<property name="hibernate.connection.url" value="jdbc:postgresql://localhost:5432/infodb" />
<property name="hibernate.connection.driver_class" value="org.postgresql.Driver" />
<property name="hibernate.connection.username" value="postgres" />
<property name="hibernate.connection.password" value="postgres" />
<property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect" />
<property name="hibernate.hbm2ddl.auto" value="create-drop" />
</properties>
</persistence-unit>
</persistence>
Person.java
package com.dain_torson.appserver;
import javax.persistence.*;
import java.io.Serializable;
@Entity
@Table(name="persons")
public class Person implements Serializable {
private int id;
private String name;
private String group;
public Person(){
}
//mark id as primary key with autogenerated value
//map database column id with id field
@Id
@GeneratedValue(strategy= GenerationType.IDENTITY)
@Column(name="id")
public int getId() {
return id;
}
@Column(name="surname")
public String getName() {
return name;
}
@Column(name="group_num")
public String getGroup() {
return group;
}
public void setId(int id) {
this.id = id;
}
public void setName(String name) {
this.name = name;
}
public void setGroup(String group) {
this.group = group;
}
HellWorld.java
package com.dain_torson.appserver;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.Persistence;
import javax.persistence.PersistenceContext;
@Stateless
public class HelloWorld implements HelloWorldInterface
{
private EntityManager entityManager;
public HelloWorld() {
}
@Override
public String helloworld() {
entityManager = Persistence.createEntityManagerFactory("postgres").createEntityManager();
if(entityManager == null) {
return "null";
}
Person person = (Person)entityManager.createQuery("FROM Person").getSingleResult();
return person.getName();
}
}
の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>com.dain_torson.appserver</groupId>
<artifactId>StudentInfoAppServer</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<name>StudentInfoAppServer Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-jaxrs</artifactId>
<version>3.0.16.Final</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.sun.xml.ws</groupId>
<artifactId>jaxws-rt</artifactId>
<version>2.1.3</version>
</dependency>
<dependency>
<groupId>javax.ejb</groupId>
<artifactId>ejb-api</artifactId>
<version>3.0</version>
</dependency>
<dependency>
<groupId>javax.persistence</groupId>
<artifactId>persistence-api</artifactId>
<version>1.0.2</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>3.3.2.GA</version>
</dependency>
<dependency>
<groupId>javax.transaction</groupId>
<artifactId>jta</artifactId>
<version>1.1</version>
</dependency>
<dependency>
<groupId>postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>9.1-901-1.jdbc4</version>
</dependency>
</dependencies>
<build>
<finalName>StudentInfoAppServer</finalName>
<plugins>
<plugin>
<groupId>org.wildfly.plugins</groupId>
<artifactId>wildfly-maven-plugin</artifactId>
<version>1.0.2.Final</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.6</version>
<executions>
<execution>
<!-- First step is to disable the default-war build step. -->
<id>default-war</id>
<phase>none</phase>
</execution>
<execution>
<!-- Second step is to create an exploded war. Done in prepare-package -->
<id>war-exploded</id>
<phase>prepare-package</phase>
<goals>
<goal>exploded</goal>
</goals>
<configuration>
<webResources>
<resource>
<directory>src/main/resources/META-INF </directory>
<targetPath>WEB-INF/classes/META-INF</targetPath>
<filtering>true</filtering>
<includes>
<include>**/persistence.xml</include>
</includes>
</resource>
</webResources>
</configuration>
</execution>
<execution>
<!-- Last step is to make sure that the war is built in the package phase -->
<id>custom-war</id>
<phase>package</phase>
<goals>
<goal>war</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
私は数日間、これで苦労してきたので、任意の助けには理解されるであろう。
*しかし、WARの中にはpersistence.xmlは含まれていないようです... *作成されたWARを*永続性.xml *は存在しませんか?また、* hibernate-entitymanager *の依存関係をWildFlyのバージョンと一致するものにアップグレードしてみてください。あなたが使っているものは、2008年からかなり古くなっています。また、PostgreSQLの依存関係を[新しいバージョン](http://mvnrepository.com/artifact/org.postgresql/postgresql)にアップグレードしてください。 – aribeiro
@aribeiroご返信ありがとうございます。 persistence.xmlはWARにWEB-INF \ classes \ META-INFの下に含まれているようですが、サーバーはそれを見逃すだけです。 –
これらの依存関係のほとんどは、コンテナが必要に応じてそれらを追加するので、ほとんどすべての依存関係に「 」とマークする必要があります。 –