0

I am using Wildfly 9.0.2, Postgresql 9.5, Hibernate 5.1.0.Final, Java JDK 1.7.0_79

This is my persistence.xml

<persistence xmlns="http://java.sun.com/xml/ns/persistence"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
    version="2.0">
    <persistence-unit name="com.example.flow.persistence.jpa" transaction-type="RESOURCE_LOCAL">
        <description>Flow Persistence Unit</description>
        <provider>org.hibernate.ejb.HibernatePersistence</provider>
        <properties>
            <property name="hibernate.archive.autodetection" value="class" />
            <property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect" />
            <property name="hibernate.connection.driver_class" value="org.postgresql.Driver" />
            <property name="hibernate.connection.url" value="jdbc:postgresql://localhost:5431/test_db" />
            <property name="hibernate.connection.username" value="postgres" />
            <property name="hibernate.connection.password" value="password" />
            <property name="hibernate.show_sql" value="true"/>
            <property name="hibernate.flushMode" value="FLUSH_AUTO" />
            <property name="hibernate.hbm2ddl.auto" value="validate" />
        </properties>
    </persistence-unit>
</persistence>

This is my 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/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.example</groupId>
    <artifactId>flow</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>
    <name>Flow</name>
    <description>To catch cockroach</description>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>2.6</version>
                <configuration>
                    <failOnMissingWebXml>false</failOnMissingWebXml>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <source>1.7</source>
                    <target>1.7</target>
                </configuration>
            </plugin>
        </plugins>
    </build>

    <dependencies>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-entitymanager</artifactId>
            <version>5.1.0.Final</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-validator</artifactId>
            <version>5.2.4.Final</version>
        </dependency>

        <dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <version>9.4.1208.jre6</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>

    </dependencies>
</project>

And a User.java entity class

package my.com.oguniform.flow.model;

import javax.persistence.Entity;
import java.io.Serializable;
import javax.persistence.Table;
import javax.persistence.Id;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Column;
import javax.persistence.Version;

@Entity
@Table(name = "app_user")
public class User implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id", updatable = false, nullable = false)
    private Long id;

    @Version
    @Column(name = "version")
    private int version;

    public Long getId() {
        return this.id;
    }

    public void setId(final Long id) {
        this.id = id;
    }

    public int getVersion() {
        return this.version;
    }

    public void setVersion(final int version) {
        this.version = version;
    }

    @Override
    public String toString() {
        String result = getClass().getSimpleName() + " ";
        if (id != null)
            result += "id: " + id;
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (!(obj instanceof User)) {
            return false;
        }
        User other = (User) obj;
        if (id != null) {
            if (!id.equals(other.id)) {
                return false;
            }
        }
        return true;
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((id == null) ? 0 : id.hashCode());
        return result;
    }
}

After deploy, I am getting this error:

17:01:33,822 INFO  [org.wildfly.extension.undertow] (ServerService Thread Pool -- 171) WFLYUT0022: Unregistered web context: /flow
17:01:33,835 INFO  [org.jboss.as.jpa] (ServerService Thread Pool -- 172) WFLYJPA0011: Stopping Persistence Unit (phase 2 of 2) Service 'flow.war#com.example.flow.persistence.jpa'
17:01:33,841 INFO  [org.jboss.as.jpa] (ServerService Thread Pool -- 172) WFLYJPA0011: Stopping Persistence Unit (phase 1 of 2) Service 'flow.war#com.example.flow.persistence.jpa'
17:01:33,863 INFO  [org.jboss.as.server.deployment] (MSC service thread 1-5) WFLYSRV0028: Stopped deployment flow.war (runtime-name: flow.war) in 45ms
17:01:33,865 INFO  [org.jboss.as.server.deployment] (MSC service thread 1-4) WFLYSRV0027: Starting deployment of "flow.war" (runtime-name: "flow.war")
17:01:33,943 INFO  [org.jboss.as.jpa] (MSC service thread 1-4) WFLYJPA0002: Read persistence.xml for com.example.flow.persistence.jpa
17:01:33,964 INFO  [org.jboss.as.jpa] (ServerService Thread Pool -- 172) WFLYJPA0010: Starting Persistence Unit (phase 1 of 2) Service 'flow.war#com.example.flow.persistence.jpa'
17:01:33,964 INFO  [org.hibernate.jpa.internal.util.LogHelper] (ServerService Thread Pool -- 172) HHH000204: Processing PersistenceUnitInfo [
    name: com.example.flow.persistence.jpa
    ...]
17:01:33,994 INFO  [org.jboss.as.jpa] (ServerService Thread Pool -- 172) WFLYJPA0010: Starting Persistence Unit (phase 2 of 2) Service 'flow.war#com.example.flow.persistence.jpa'
17:01:33,997 INFO  [org.hibernate.dialect.Dialect] (ServerService Thread Pool -- 172) HHH000400: Using dialect: org.hibernate.dialect.PostgreSQLDialect
17:01:34,000 INFO  [org.hibernate.hql.internal.ast.ASTQueryTranslatorFactory] (ServerService Thread Pool -- 172) HHH000397: Using ASTQueryTranslatorFactory
17:01:34,009 INFO  [org.hibernate.tool.hbm2ddl.SchemaValidator] (ServerService Thread Pool -- 172) HHH000229: Running schema validator
17:01:34,009 INFO  [org.hibernate.tool.hbm2ddl.SchemaValidator] (ServerService Thread Pool -- 172) HHH000102: Fetching database metadata
17:01:34,009 ERROR [org.hibernate.tool.hbm2ddl.SchemaValidator] (ServerService Thread Pool -- 172) HHH000319: Could not get database metadata: org.h2.jdbc.JdbcSQLException: Table "PG_CLASS" not found; SQL statement:
select relname from pg_class where relkind='S' [42102-173]
    at org.h2.message.DbException.getJdbcSQLException(DbException.java:331)
    at org.h2.message.DbException.get(DbException.java:171)
    at org.h2.message.DbException.get(DbException.java:148)
    at org.h2.command.Parser.readTableOrView(Parser.java:4864)
    at org.h2.command.Parser.readTableFilter(Parser.java:1107)
    at org.h2.command.Parser.parseSelectSimpleFromPart(Parser.java:1713)
    at org.h2.command.Parser.parseSelectSimple(Parser.java:1821)
    at org.h2.command.Parser.parseSelectSub(Parser.java:1707)
    at org.h2.command.Parser.parseSelectUnion(Parser.java:1550)
    at org.h2.command.Parser.parseSelect(Parser.java:1538)
    at org.h2.command.Parser.parsePrepared(Parser.java:405)
    at org.h2.command.Parser.parse(Parser.java:279)
    at org.h2.command.Parser.parse(Parser.java:251)
    at org.h2.command.Parser.prepareCommand(Parser.java:218)
    at org.h2.engine.Session.prepareLocal(Session.java:428)
    at org.h2.engine.Session.prepareCommand(Session.java:377)
    at org.h2.jdbc.JdbcConnection.prepareCommand(JdbcConnection.java:1138)
    at org.h2.jdbc.JdbcStatement.executeQuery(JdbcStatement.java:72)
    at org.jboss.jca.adapters.jdbc.WrappedStatement.executeQuery(WrappedStatement.java:381)
    at org.hibernate.tool.hbm2ddl.DatabaseMetadata.initSequences(DatabaseMetadata.java:178)
    at org.hibernate.tool.hbm2ddl.DatabaseMetadata.<init>(DatabaseMetadata.java:92)
    at org.hibernate.tool.hbm2ddl.SchemaValidator.validate(SchemaValidator.java:168)
    at org.hibernate.internal.SessionFactoryImpl.<init>(SessionFactoryImpl.java:525)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1859)
    at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl$4.perform(EntityManagerFactoryBuilderImpl.java:857)
    at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl$4.perform(EntityManagerFactoryBuilderImpl.java:850)
    at org.hibernate.boot.registry.classloading.internal.ClassLoaderServiceImpl.withTccl(ClassLoaderServiceImpl.java:425)
    at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:849)
    at org.jboss.as.jpa.hibernate4.TwoPhaseBootstrapImpl.build(TwoPhaseBootstrapImpl.java:44)
    at org.jboss.as.jpa.service.PersistenceUnitServiceImpl$1$1.run(PersistenceUnitServiceImpl.java:154)
    at org.jboss.as.jpa.service.PersistenceUnitServiceImpl$1$1.run(PersistenceUnitServiceImpl.java:117)
    at org.wildfly.security.manager.WildFlySecurityManager.doChecked(WildFlySecurityManager.java:665)
    at org.jboss.as.jpa.service.PersistenceUnitServiceImpl$1.run(PersistenceUnitServiceImpl.java:182)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)
    at org.jboss.threads.JBossThread.run(JBossThread.java:320)

17:01:34,010 ERROR [org.hibernate.tool.hbm2ddl.SchemaValidator] (ServerService Thread Pool -- 172) HHH000300: Could not complete schema validation: org.h2.jdbc.JdbcSQLException: Table "PG_CLASS" not found; SQL statement:
select relname from pg_class where relkind='S' [42102-173]
    at org.h2.message.DbException.getJdbcSQLException(DbException.java:331)
    at org.h2.message.DbException.get(DbException.java:171)
    at org.h2.message.DbException.get(DbException.java:148)
    at org.h2.command.Parser.readTableOrView(Parser.java:4864)
    at org.h2.command.Parser.readTableFilter(Parser.java:1107)
    at org.h2.command.Parser.parseSelectSimpleFromPart(Parser.java:1713)
    at org.h2.command.Parser.parseSelectSimple(Parser.java:1821)
    at org.h2.command.Parser.parseSelectSub(Parser.java:1707)
    at org.h2.command.Parser.parseSelectUnion(Parser.java:1550)
    at org.h2.command.Parser.parseSelect(Parser.java:1538)
    at org.h2.command.Parser.parsePrepared(Parser.java:405)
    at org.h2.command.Parser.parse(Parser.java:279)
    at org.h2.command.Parser.parse(Parser.java:251)
    at org.h2.command.Parser.prepareCommand(Parser.java:218)
    at org.h2.engine.Session.prepareLocal(Session.java:428)
    at org.h2.engine.Session.prepareCommand(Session.java:377)
    at org.h2.jdbc.JdbcConnection.prepareCommand(JdbcConnection.java:1138)
    at org.h2.jdbc.JdbcStatement.executeQuery(JdbcStatement.java:72)
    at org.jboss.jca.adapters.jdbc.WrappedStatement.executeQuery(WrappedStatement.java:381)
    at org.hibernate.tool.hbm2ddl.DatabaseMetadata.initSequences(DatabaseMetadata.java:178)
    at org.hibernate.tool.hbm2ddl.DatabaseMetadata.<init>(DatabaseMetadata.java:92)
    at org.hibernate.tool.hbm2ddl.SchemaValidator.validate(SchemaValidator.java:168)
    at org.hibernate.internal.SessionFactoryImpl.<init>(SessionFactoryImpl.java:525)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1859)
    at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl$4.perform(EntityManagerFactoryBuilderImpl.java:857)
    at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl$4.perform(EntityManagerFactoryBuilderImpl.java:850)
    at org.hibernate.boot.registry.classloading.internal.ClassLoaderServiceImpl.withTccl(ClassLoaderServiceImpl.java:425)
    at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:849)
    at org.jboss.as.jpa.hibernate4.TwoPhaseBootstrapImpl.build(TwoPhaseBootstrapImpl.java:44)
    at org.jboss.as.jpa.service.PersistenceUnitServiceImpl$1$1.run(PersistenceUnitServiceImpl.java:154)
    at org.jboss.as.jpa.service.PersistenceUnitServiceImpl$1$1.run(PersistenceUnitServiceImpl.java:117)
    at org.wildfly.security.manager.WildFlySecurityManager.doChecked(WildFlySecurityManager.java:665)
    at org.jboss.as.jpa.service.PersistenceUnitServiceImpl$1.run(PersistenceUnitServiceImpl.java:182)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)
    at org.jboss.threads.JBossThread.run(JBossThread.java:320)

17:01:34,011 WARN  [org.hibernate.internal.SessionFactoryImpl] (ServerService Thread Pool -- 172) HHH000008: JTASessionContext being used with JDBCTransactionFactory; auto-flush will not operate correctly with getCurrentSession()
17:01:34,025 INFO  [org.wildfly.extension.undertow] (ServerService Thread Pool -- 173) WFLYUT0021: Registered web context: /flow
17:01:34,135 INFO  [org.jboss.as.server] (DeploymentScanner-threads - 2) WFLYSRV0016: Replaced deployment "flow.war" with deployment "flow.war"
17:01:34,135 INFO  [org.jboss.as.controller] (DeploymentScanner-threads - 2) WFLYCTL0183: Service status report
WFLYCTL0186:   Services which failed to start:      service jboss.serverManagement.controller.management.http: org.jboss.msc.service.StartException in service jboss.serverManagement.controller.management.http: WFLYSRV0083: Failed to start the http-interface service

This is where I got confused. I already setup postgres driver etc, but in the error logs, I saw that Wildfly try to connect using h2 driver.

If I changed the hbm2ddl.auto to 'create', I can see in the log that the table 'app_user' is created, but not appear in my postgresql table, so I suspect it is created in H2 instead.

Please help.

VHanded
  • 2,079
  • 4
  • 30
  • 55
  • Where is the code where you specify which persistence unit to use? (@PersistenceContext(unitName = "MyUnit")) ... I also set up the connection in wildfly, and therefore do not provide connection string, username and password in persistence.xml, just the name of the connection. (Other hibernate properties are specified in persistence.xml though, like dialect, formatting of sql and hibernate.hbm2ddl.auto. But first, just try to annotate EntityManager in your services with the above annotation, maybe that's enough. – jon martin solaas Mar 20 '16 at 10:53
  • Hi @jonmartinsolaas, in my log, it seems like it is selecting 'com.example.flow.persistence.jpa' as my persistence-unit, which is my only persistence-unit. – VHanded Mar 20 '16 at 12:13
  • I think it's *very* odd that h2 is used if your postgresql persistence unit is actually connected to your EntityManager. When persistence.xml is read, wildfly will log the persistence units found there. But in order to actually use it in your code you must point your EntityManager at it too, it won't happen automatically. Try to set up driver and connection like Nicholas has described below, and then annotate your EntityManager with the @PersistenceContext annotation and unitName parameter. (I think I got much clearer error messages when I was struggling with Glassfish and connections ... ) – jon martin solaas Mar 20 '16 at 17:55

2 Answers2

4

You need to add the postgresql jdbc driver as module and datasource to Wildfly since Wildfly doesn't have it installed out of the box.

You can do this with the wildfly command line from the wildfly bin directory:

./jboss-cli.sh

Then add the module:

module add --name=org.postgres --resources=/tmp/postgresql-9.3-1101.jdbc41.jar --dependencies=javax.api,javax.transaction.api

Then install the driver on the server :

/subsystem=datasources/jdbc-driver=postgres:add(driver-name="postgres",driver-module-name="org.postgres",driver-class-name=org.postgresql.Driver)

Then configure the datasource: Note this step has the JNDI name that needs to match your persistence.xml.

data-source add --jndi-name=java:/PostGreDS --name=PostgrePool --connection-url=jdbc:postgresql://localhost/postgres --driver-name=postgres --user-name=postgres --password=postgres

After it is tested, you can do some tuning as per this reference and tutorial:

Configuring a datasource with PostgreSQL and JBoss/WildFly.

See also: How to add PostgreSQL datasource to WildFly 9.0?

UPDATE: You also need to update your persistence.xml to use the Container Managed Datasource:

<persistence-unit name="prod" transaction-type="JTA">
<jta-data-source>jdbc/sample</jta-data-source>

The difference is between an Application vs. a Container Managed EntityManager. Since you are deploying to a container (Wildfly), you want to use a container managed EntityManager. Reference: Container vs Application Managed EntityManager

Community
  • 1
  • 1
K.Nicholas
  • 10,956
  • 4
  • 46
  • 66
  • OK.... I thought by applying all credentials and connection in persistence.xml is sufficient... Why..? – VHanded Mar 20 '16 at 15:20
  • I think it is kind of odd that widlfly just defaults to use h2 when the postgresql driver and connection is missing? I'd expect a bunch of error messages ... Adding driver and datasource as described above seems correct, but I'm not sure it's enough to convince wildfly to let go of h2 ... – jon martin solaas Mar 20 '16 at 17:51
  • Yea, I think it's odd too, but I didn't see enough information in there to attempt to debug that. You can see in the logs that it starts up a `PostgreSQL` dialiect, but it's impossible to know everything that's going on in his application server. – K.Nicholas Mar 20 '16 at 17:58
  • I think wildfly defaults to use the preconfigured h2 persistence unit unless anything else is specified. The postgredsql persistence unit is clearly registered, because it appears in the log, but the EntityManager must also be configured to use it. – jon martin solaas Mar 20 '16 at 18:05
  • Well, the h2 persistence unit is tied to a JNDI name (java/ExampleDS), so unless something references that it won't get activated. – K.Nicholas Mar 20 '16 at 18:15
  • Hi @nicholas, what is the 'something' that reference it, usually? – VHanded Mar 21 '16 at 00:51
  • Well, I would think another deployment, but I don't see anything obvious in your logs, so I'm not sure what it is. – K.Nicholas Mar 21 '16 at 02:32
  • 1
    You don't specify which datasource to use so it will fall back to the default one (since Java EE7) which is H2 <. Also I think you should use org.hibernate.jpa.HibernatePersistenceProvider – ehsavoie Mar 22 '16 at 11:38
0

First Follow pom.xml:

<?xml version="1.0" encoding="UTF-8"?>

http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0

<groupId>com.crossover</groupId>
<artifactId>compiler-web-application</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <version.jboss.bom>10.0.0.Final</version.jboss.bom>
    <version.wildfly.maven.plugin>1.0.2.Final</version.wildfly.maven.plugin>
    <version.war.plugin>2.1.1</version.war.plugin>
    <maven.compiler.target>1.8</maven.compiler.target>
    <maven.compiler.source>1.8</maven.compiler.source>
</properties>

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.wildfly.bom</groupId>
            <artifactId>wildfly-javaee7-with-tools</artifactId>
            <version>${version.jboss.bom}</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

<dependencies>

    <dependency>
        <groupId>javax.enterprise</groupId>
        <artifactId>cdi-api</artifactId>
        <scope>provided</scope>
    </dependency>

    <dependency>
        <groupId>org.jboss.spec.javax.annotation</groupId>
        <artifactId>jboss-annotations-api_1.2_spec</artifactId>
        <scope>provided</scope>
    </dependency>

    <dependency>
        <groupId>org.jboss.spec.javax.ws.rs</groupId>
        <artifactId>jboss-jaxrs-api_2.0_spec</artifactId>
        <scope>provided</scope>
    </dependency>

    <dependency>
        <groupId>org.hibernate.javax.persistence</groupId>
        <artifactId>hibernate-jpa-2.1-api</artifactId>
        <scope>provided</scope>
    </dependency>

    <dependency>
        <groupId>org.jboss.spec.javax.ejb</groupId>
        <artifactId>jboss-ejb-api_3.2_spec</artifactId>
        <scope>provided</scope>
    </dependency>

    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-core</artifactId>
        <scope>provided</scope>
    </dependency>

    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-validator</artifactId>
        <scope>provided</scope>
        <exclusions>
            <exclusion>
                <groupId>org.slf4j</groupId>
                <artifactId>slf4j-api</artifactId>
            </exclusion>
        </exclusions>
    </dependency>

    <dependency>
        <groupId>org.jboss.spec.javax.faces</groupId>
        <artifactId>jboss-jsf-api_2.2_spec</artifactId>
        <scope>provided</scope>
    </dependency>

    <!-- HELPERS -->
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <version>1.12.2</version>
    </dependency>

    <!-- TESTES -->
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.11</version>
    </dependency>


    <!-- Primefaces + FileUpload -->
    <dependency>
        <groupId>org.primefaces</groupId>
        <artifactId>primefaces</artifactId>
        <version>6.0</version>
    </dependency>
    <dependency>
        <groupId>org.primefaces.extensions</groupId>
        <artifactId>primefaces-extensions</artifactId>
        <version>6.0.0</version>
    </dependency>
    <dependency>
        <groupId>org.primefaces.themes</groupId>
        <artifactId>bootstrap</artifactId>
        <version>1.0.10</version>
    </dependency>

</dependencies>

<build>
    <finalName>${project.artifactId}</finalName>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.1</version>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
            </configuration>
        </plugin>
        <plugin>
            <artifactId>maven-war-plugin</artifactId>
            <version>${version.war.plugin}</version>
            <configuration>
                <failOnMissingWebXml>false</failOnMissingWebXml>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.wildfly.plugins</groupId>
            <artifactId>wildfly-maven-plugin</artifactId>
            <version>${version.wildfly.maven.plugin}</version>
        </plugin>
    </plugins>
</build>

<repositories>
    <repository>
        <id>apache.snapshots</id>
        <name>Apache Maven Snapshot Repository</name>
        <url>http://people.apache.org/repo/m2-snapshot-repository</url>
    </repository>
    <repository>
        <id>prime-repo</id>
        <name>PrimeFaces Maven Repository</name>
        <url>http://repository.primefaces.org</url>
        <layout>default</layout>
    </repository>
</repositories>

<!--If want use openshift -->
<!--<profiles>-->
<!--<profile>-->
<!--<id>openshift</id>-->
<!--<build>-->
<!--<plugins>-->
<!--<plugin>-->
<!--<artifactId>maven-war-plugin</artifactId>-->
<!--<version>${version.war.plugin}</version>-->
<!--<configuration>-->
<!--<outputDirectory>deployments</outputDirectory>-->
<!--<warName>ROOT</warName>-->
<!--</configuration>-->
<!--</plugin>-->
<!--</plugins>-->
<!--</build>-->
<!--</profile>-->
<!--</profiles>-->

  • Now you need configure the postgresql

For do this:

  • Download de jar of module ( example : postgresql-9.4.1209.jre6.jar)
  • Add the new module in your wildfly : create the dir postgresql/main/

/wildfly-10.0.0.Final/modules/system/layers/base/org/postgresql/main/

-- create inside the file module.xml

<?xml version="1.0" encoding="UTF-8"?>
<module xmlns="urn:jboss:module:1.3" name="org.postgresql">
  <resources>
    <resource-root path="postgresql-9.4.1209.jre6.jar"/>
  </resources>
  <dependencies>
      <module name="javax.api"/>
      <module name="javax.transaction.api"/>
   </dependencies>
</module>

-- Now add the new configuration in the standalone.xml file.

/wildfly/wildfly-10.0.0.Final/standalone/configuration/standalone.xml

<datasource jndi-name="java:jboss/datasources/PostgresDS" pool-name="PostgresDS" enabled="true" use-java-context="true">
                <connection-url>jdbc:postgresql://localhost:5432/dbname</connection-url>
                <driver>postgresql</driver>
                <transaction-isolation>TRANSACTION_READ_COMMITTED</transaction-isolation>
                <pool>
                    <min-pool-size>5</min-pool-size>
                    <max-pool-size>30</max-pool-size>
                    <prefill>true</prefill>
                    <use-strict-min>false</use-strict-min>
                    <flush-strategy>FailingConnectionOnly</flush-strategy>
                </pool>
                <security>
                    <user-name>postgres</user-name>
                    <password>root</password>
                </security>
                <statement>
                    <prepared-statement-cache-size>32</prepared-statement-cache-size>
                </statement>
            </datasource>

-- add the driver to in the same file:

 <driver name="postgresql" module="org.postgres">
                    <xa-datasource-class>org.postgresql.Driver</xa-datasource-class>
                </driver>

-- Your persistence.xml will be :

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<persistence xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xmlns="http://xmlns.jcp.org/xml/ns/persistence"
             xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd"
             version="2.1">
    <persistence-unit name="compilerPU" transaction-type="JTA">
        <description>uptake pu</description>
        <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
        <jta-data-source>java:jboss/datasources/PostgresDS</jta-data-source>
        <exclude-unlisted-classes>false</exclude-unlisted-classes>
        <shared-cache-mode>ENABLE_SELECTIVE</shared-cache-mode>

        <!--&lt;!&ndash; provedor/implementacao do JPA &ndash;&gt;-->
        <!--<provider>org.hibernate.ejb.HibernatePersistence</provider>-->

        <!--&lt;!&ndash; entidade mapeada &ndash;&gt;-->
        <!--<class>model.User</class>-->

        <properties>

            <property name="hibernate.transaction.flush_before_completion" value="true"/>
            <property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQL9Dialect"/>

            <property name="hibernate.cache.use_second_level_cache" value="true"/>
            <property name="hibernate.cache.use_query_cache" value="true"/>
            <property name="hibernate.jdbc.batch_size" value="50"/>
            <property name="hibernate.max_fetch_depth" value="0"/>
            <property name="hibernate.hbm2ddl.auto" value="create-drop"/>
            <property name="hibernate.show_sql" value="true"/>
            <property name="hibernate.format_sql" value="true"/>
            <!-- se for dar lazy vai buscar separamente -->
            <property name="hibernate.enable_lazy_load_no_trans" value="true"/>
        </properties>

    </persistence-unit>


</persistence>

IMPORTANT : the persistence need be inside of META-INF dir of your application.

Camila Macedo
  • 857
  • 7
  • 10