Wildfly 10与Postgres 9.6.6和Hibernate 5.2.12.final

问题描述 投票:2回答:2

我试图让一个Java8EE应用程序在Wildfly10和Postgres 9.6.6上运行。

不知何故,我总是遇到无法找到关系的错误。

我的Postgres运行在localhost,默认端口。我正在连接用户postgres和正确的密码。数据库(hbnac)具有相同名称的模式。

Wildfly配置为使用数据库,“测试连接”确认连接成功。使用pgAdmin 4我也可以浏览数据库,查看模式以及表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>

但是,我的应用程序无法从同一个表中选择一条记录,导致以下错误:

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,hashcode和equals方法。

如果我将hibernate生成的查询复制到pgadmin中,它也可以完美地工作......

有什么想法吗?

A screenshot from the actual db in pgadmin

postgresql hibernate wildfly
2个回答
0
投票

测试连接成功,但即使找不到表示的表,也许架构设置未正确定义。那么,应该像这样放一个currentSchema

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

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

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

0
投票

经过多次尝试,我发现了错误!它是由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>
     ...
© www.soinside.com 2019 - 2024. All rights reserved.