2016-04-07 60 views
2

我試圖使用WildFly和Maven運行一個簡單的EJB示例,但卡在舞臺上,我應該創建一個持久性單元。 因此,我創建了PostgreSQL數據庫,配置了Wildfly數據源並編寫了一個persistence.xml文件。但是,這似乎是戰爭,沒包括persistence.xml中,所以每次我嘗試創建一個EntityManager,我得到以下信息和錯誤從Wildfly:無法在類路徑中找到任何META-INF/persistence.xml文件(wildfly,maven)

11:55:57664 INFO [org.hibernate.jpa.boot。 ERROR [io.undertow.request](默認任務-1)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:否EntityManager的持久性提供程序,名爲postgres

我的persistence.xml目前在src/META-INF文件夾(其中思想把它當我創建持久性單元),但我也試着:

  • 的src/main/JAVA/META-INF
  • 的src /主/ web應用/ 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> 

我一直在掙扎了這幾天,所以任何幫助將不勝感激。

+0

*但它似乎是戰爭,沒包括的persistence.xml ... *您是否嘗試過以尋找所創建的WAR中,看看是否*持久性.xml *不存在?另外,請嘗試將* hibernate-entitymanager *依賴項升級到與您的WildFly版本匹配的版本。從2008年開始,使用它的人已經很老了。還要將PostgreSQL依賴關係升級到[新版本](http://mvnrepository.com/artifact/org.postgresql/postgresql)。 – aribeiro

+0

@aribeiro感謝您的回覆。看來,persistence.xml包含在WAR下的WEB-INF \ classes \ META-INF中,但服務器只是拒絕看到它。 –

+1

幾乎所有這些依賴應該被標記爲'提供'因爲容器將根據需要添加那些。 –

回答

0

這是關於如何在pom.xml中配置WAR的構建。它不會自動執行。我在src/main/resources/META-INF中有我的。但關鍵是那麼我如何配置我的pom.xml:

<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> 
+0

感謝您的快速回復!我已經將META-INF移動到資源並添加了您提供的配置,但是我仍然收到相同的錯誤。 _Btw,我添加了我的pom。XML的問題._ –

相關問題