【问题标题】:Wildfly 10 with Postgres 9.6.6 and Hibernate 5.2.12.finalWildfly 10 与 Postgres 9.6.6 和 Hibernate 5.2.12.final
【发布时间】:2018-06-08 12:10:48
【问题描述】:

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

我总是遇到找不到关系的错误。

我的 Postgres 在本地主机上运行,​​默认端口。我正在连接用户 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 中,它也可以正常工作...

还有什么想法吗?

【问题讨论】:

    标签: postgresql hibernate wildfly


    【解决方案1】:

    测试连接成功,但连表都找不到 这表明,也许架构设置没有正确定义。 那么,应该像这样放置一个currentSchema

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

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

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

    【讨论】:

    • 我已经尝试了这些设置,还更改了数据库级别的搜索路径(ALTER DATABASE hbnac SET search_path TO hbnac;),但没有任何帮助......我真的认为我是受害者某种错误...
    【解决方案2】:

    经过多次尝试,我发现了错误!是 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>
         ...
    

    【讨论】:

      猜你喜欢
      • 2016-07-13
      • 1970-01-01
      • 1970-01-01
      • 2016-05-31
      • 2017-03-14
      • 1970-01-01
      • 2015-04-25
      • 2017-07-20
      • 2016-10-02
      相关资源
      最近更新 更多