在类路径中找不到任何META-INF / persistence.xml文件(wildfly,maven)

时间:2016-04-07 18:22:58

标签: java postgresql maven jpa wildfly

我试图使用WildFly和Maven运行一个简单的EJB示例,但仍然停留在舞台上,我应该创建一个持久性单元。 所以,我创建了一个PostgreSQL数据库,配置了Wildfly数据源并编写了一个persistence.xml文件。但似乎WAR永远不会包含persistence.xml,所以每当我尝试创建一个EntityManager时,我都会从Wildfly获得以下信息和错误:

  

11:55:57,664 INFO [org.hibernate.jpa.boot.internal.PersistenceXmlParser](默认任务-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:没有名为postgres的EntityManager的持久性提供程序

我的persistence.xml目前位于src / META-INF文件夹下(当我创建持久性单元时Idea将其放入),但我也尝试过:

  • 的src /主/ 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>

我几天来一直在努力,所以任何帮助都会受到赞赏。

1 个答案:

答案 0 :(得分: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>