带有 Postgres 9.6.6 和 Hibernate 5.2 的 Wildfly 10。12.final

Wildfly 10 with Postgres 9.6.6 and Hibernate 5.2.12.final

我正在尝试让 Java8EE 应用程序在 Wildfly10 和 Postgres 9.6.6 上运行。

不知怎么的,我总是遇到找不到关系的错误。

我的 Postgres 运行 在本地主机上,默认端口。我正在连接用户 postgres 和正确的密码。数据库 (hbnac) 有一个同名的模式。

Wildfly 配置为使用数据库,“测试连接”确认连接成功。 使用 pgAdmin 4 我还可以浏览数据库、查看架构以及 table group_member.

Jboss 中的配置如下所示:

<datasource jta="false" jndi-name="java:jboss/datasources/hbnac" pool-name="hbnac" enabled="true" use-ccm="false">
                <connection-url>jdbc:postgresql://localhost:5432/hbnac</connection-url>
                <driver-class>org.postgresql.Driver</driver-class>
                <driver>postgres</driver>
                <pool>
                    <min-pool-size>1</min-pool-size>
                    <initial-pool-size>1</initial-pool-size>
                    <max-pool-size>10</max-pool-size>
                    <flush-strategy>Gracefully</flush-strategy>
                </pool>
                <security>
                    <user-name>postgres</user-name>
                    <password>password</password>
                </security>
                <validation>
                    <valid-connection-checker class-name="org.jboss.jca.adapters.jdbc.extensions.postgres.PostgreSQLValidConnectionChecker"/>
                    <background-validation>true</background-validation>
                    <exception-sorter class-name="org.jboss.jca.adapters.jdbc.extensions.postgres.PostgreSQLExceptionSorter"/>
                </validation>
            </datasource>

但是,我的应用程序无法 select 来自同一个 table 的记录,导致以下错误:

Caused by: org.postgresql.util.PSQLException: ERROR: relation "group_member" does not exist

我的 persistence.xml 配置正在对 Wildfly 中定义的连接进行 jndi 查找:

<persistence-unit name="hbnac">
    <jta-data-source>java:jboss/datasources/hbnac</jta-data-source>

    <class>hbnac.birthday.domain.groupmember.GroupMember</class>

    <properties>
        <property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQL94Dialect" />
        <!--property name="hibernate.hbm2ddl.auto" value="validate"/-->
        <!--property name="hibernate.default_schema" value="hbnac"/-->

        <property name="hibernate.show_sql" value="true"/>
        <property name="hibernate.format_sql" value="true" />
    </properties>

</persistence-unit>

显然,jta-data-source 与 Wildfly 中的工作数据源匹配。如您所见,我对验证(在启动时出于同样的原因失败)和模式名称进行了一些试验,这也没有什么区别。

实体本身被注释:

@Entity
@Table(name = "group_member")
public class GroupMember extends AbstractEntity {

public final static String FACEBOOK_NAME_FIELD = "facebookName";
public final static String BIRTHDAY_FIELD = "birthday";

@Id
@GeneratedValue(strategy= GenerationType.IDENTITY)
private long id;

它具有适用于所有可用字段的 getter、setter、哈希码和等于方法。

如果我将 hibernate 生成的查询复制到 pgadmin 中,它也能正常工作...

还有什么想法吗?

测试连接成功,但还是找不到table 这表明,也许架构设置未正确定义。 然后,应该像这样放置一个 currentSchema

<connection-url>jdbc:postgresql://localhost:5432/hbnac?currentSchema=hbnac</connection-url>

如果您使用 Hibernate 发出查询,则应启用此注释代码

<!--property name="hibernate.default_schema" value="hbnac"/-->

多次尝试后发现错误!是Wildfly本身配置Postgres模块造成的

我之前有:

<?xml version="1.0" encoding="UTF-8"?>
<module xmlns="urn:jboss:module:1.1" name="org.postgres">
    <resources>
        <resource-root path="postgresql-42.1.4.jar" />
    </resources>
    <dependencies>
        <module name="javax.api"/>
        <module name="javax.transaction.api"/>
        <module name="javax.servlet.api" optional="true" />
    </dependencies>

并在 standalone.xml 中:

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

对于数据源本身:

 <datasource jta="false" jndi-name="java:jboss/datasources/hbnac" pool-name="hbnac" enabled="true" use-ccm="false">
                <connection-url>jdbc:postgresql://localhost:5432/hbnac</connection-url>
                <driver-class>org.postgresql.Driver</driver-class>
                <driver>postgres</driver>
                ...

此配置给出了一个成功的连接但是找不到关系的错误。

将其替换为以下配置,一切正常:

对于module.xml:

<?xml version="1.0" encoding="UTF-8"?>
<module xmlns="urn:jboss:module:1.1" name="org.postgres">
<resources>
    <resource-root path="postgresql-42.1.4.jar"/>
</resources>
<dependencies>
    <module name="javax.api"/>
    <module name="javax.transaction.api"/>
    <module name="javax.servlet.api" optional="true"/>
</dependencies>

对于standalone.xml中的驱动程序:

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

最后是数据库连接:

 <datasource jta="true" jndi-name="java:/PostgresDS" pool-name="PostgresDS" enabled="true" use-java-context="true">
     <connection-url>jdbc:postgresql://localhost:5432/hbnac</connection-url>
     <driver>postgresql</driver>
     <security>
     ...