2

I was trying to run a simple EJB example using WildFly and Maven, but stuck on the stage, where I supposed to create a persistence unit. So, I've created a PostgreSQL database, configured Wildfly datasource and written a persistence.xml file. But It seems like WAR never includes persistence.xml, so everytime I try to create an EntityManager I get the following info and error from Wildfly:

11:55:57,664 INFO [org.hibernate.jpa.boot.internal.PersistenceXmlParser] (default task-1) HHH000318: Could not find any META-INF/persistence.xml file in the classpath

11:55:57,666 ERROR [io.undertow.request] (default task-1) UT005023: Exception handling request to /StudentInfoAppServer/rest/tutorial/helloworld: org.jboss.resteasy.spi.UnhandledException: javax.persistence.PersistenceException: No Persistence provider for EntityManager named postgres

My persistence.xml is currently under src/META-INF folder (where Idea put it when I created Persistence Unit), but I've also tried:

  • src/main/java/META-INF
  • src/main/webapp/WEB-INF/classes/META-INF
  • src/main/resources/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>

I've been struggling with this for few days, so any help will be appreciated.

Community
  • 1
  • 1
Dain_Torson
  • 17
  • 1
  • 6
  • *But It seems like WAR never includes persistence.xml...* Have you tried looking inside the created WAR in order to see if the *persistence.xml* isn't present? Also, try upgrading your *hibernate-entitymanager* dependency to the one that matches your WildFly version. The one you're using it's quite old since it dates from 2008. Also upgrade your PostgreSQL dependency to the [new version](http://mvnrepository.com/artifact/org.postgresql/postgresql). – António Ribeiro Apr 08 '16 at 07:20
  • @aribeiro Thank you for the reply. It appears that persistence.xml is included to WAR under WEB-INF\classes\META-INF, but server just refuse to see it. – Dain_Torson Apr 08 '16 at 15:52
  • 1
    Nearly all those dependencies should be marked as `provided` as the container will add those as needed. – James R. Perkins Apr 08 '16 at 16:29
  • @АлесьВешторт, I think your problem is related with *hibernate-entitymanager.jar*. Which version of WildFly are you using? – António Ribeiro Apr 08 '16 at 19:57

1 Answers1

2

This is all about how you've configured the building of your WAR in your pom.xml. It doesn't do it automatically. I've got mine in src/main/resources/META-INF. But the key is then how I configure my 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>
stdunbar
  • 16,263
  • 11
  • 31
  • 53
  • Thank you for quick reply! I've moved META-INF to resources and added configs, which you had provided, but I'm still getting the same error. _Btw, I've added my pom.xml to question._ – Dain_Torson Apr 07 '16 at 19:24