无法在Wildfly上使用Hibernate创建Postgresql表

时间:2016-03-20 09:16:43

标签: hibernate postgresql wildfly wildfly-9 postgresql-9.5

我正在使用Wildfly 9.0.2,Postgresql 9.5,Hibernate 5.1.0.Final,Java JDK 1.7.0_79

这是我的persistence.xml

ex

这是我的pom.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>

User.java实体类

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

部署后,我收到此错误:

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;
    }
}

这是我感到困惑的地方。我已经设置了postgres驱动程序等,但在错误日志中,我看到Wildfly尝试使用h2驱动程序进行连接。

如果我将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 更改为&#39;创建&#39;,我可以在日志中看到该表&#39; app_user&#39;是创建的,但没有出现在我的postgresql表中,所以我怀疑它是在H2中创建的。

请帮忙。

2 个答案:

答案 0 :(得分:4)

您需要将postgresql jdbc驱动程序作为模块和数据源添加到Wildfly,因为Wildfly没有开箱即用。

您可以使用wildfly bin目录中的wildfly命令行执行此操作:

./jboss-cli.sh

然后添加模块:

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

然后在服务器上安装驱动程序:

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

然后配置数据源:注意此步骤具有需要与persistence.xml匹配的JNDI名称。

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

经过测试,您可以根据此参考和教程进行一些调整:

Configuring a datasource with PostgreSQL and JBoss/WildFly

另请参阅:How to add PostgreSQL datasource to WildFly 9.0?

更新:您还需要更新persistence.xml以使用Container Managed Datasource

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

区别在于应用程序与容器管理EntityManager之间。由于您要部署到容器(Wildfly),因此您希望使用容器管理的EntityManager。参考:Container vs Application Managed EntityManager

答案 1 :(得分:0)

首先关注pom.xml:

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

http://maven.apache.org/xsd/maven-4.0.0.xsd“&GT;     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>-->

  • 现在需要配置postgresql

为此:

  • 下载de jar模块(例如:postgresql-9.4.1209.jre6.jar)
  • 在wildfly中添加新模块: 创建dir postgresql / main /

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

- 在文件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>

- 现在在standalone.xml文件中添加新配置。

/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>

- 将驱动程序添加到同一文件中:

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

- 你的persistence.xml将是:

<?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>

重要提示:持久性需要在应用程序的META-INF目录中。