0

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

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

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

和一个 User.java 实体类

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

部署后,我收到此错误:

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

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

如果我将其更改hbm2ddl.auto为“create”,我可以在日志中看到表“app_user”已创建,但未出现在我的 postgresql 表中,因此我怀疑它是在 H2 中创建的。

请帮忙。

4

2 回答 2

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)

然后配置数据源:注意这一步的 JNDI 名称需要与您的 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

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

使用 PostgreSQL 和 JBoss/WildFly 配置数据源

另请参阅:如何将 PostgreSQL 数据源添加到 WildFly 9.0?

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

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

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

于 2016-03-20T13:17:05.917 回答
0

首先关注 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>-->

  • 现在您需要配置 postgresql

为此:

  • 下载模块的de jar(例如:postgresql-9.4.1209.jre6.jar)
  • 在您的 wildfly 中添加新模块:创建目录 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 目录中。

于 2016-08-03T13:33:02.370 回答