3

我正在使用 Wildfly 10 并使用带有 PostGIS 数据库的 Spatial Hibernate 5 开发应用程序。我在运行时收到以下错误。

java.lang.IllegalStateException: Received object of type org.postgresql.util.PGobject

谁能推荐一些关于如何将 Spatial Hibernate 与 Wildfly 10 一起使用的好教程?

4

2 回答 2

3

我有同样的问题,我刚刚解决了它。这基本上是一个依赖问题。问题是您在 Wildfly 模块和部署 WEB-INF/lib 上加载了 postgresql 和/或 postgis jar。我在我的standalone.xml 上使用常规 DS 连接到我的数据库

<datasource jndi-name="java:jboss/datasources/mygisDS" pool-name="mygisDS" use-java-context="true">
                    <connection-url>jdbc:postgresql://localhost:5432/keycloak</connection-url>
                    <driver>org.postgresql</driver>
                    <transaction-isolation>TRANSACTION_READ_COMMITTED</transaction-isolation>
                    <pool>
                        <min-pool-size>10</min-pool-size>
                        <max-pool-size>100</max-pool-size>
                        <prefill>true</prefill>
                        <use-strict-min>false</use-strict-min>
                        <flush-strategy>FailingConnectionOnly</flush-strategy>
                    </pool>
                    <security>
                        <user-name>user</user-name>
                        <password>XXXXXX</password>
                    </security>
                </datasource>

我的司机

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

正如@Toastor 所说,我尝试过,它可能解决了他的问题,但对我没有用。虽然他给了我一些线索。

所以互联网上的大多数文档都已经过时了,关于休眠空间 5 的文档并不多。我将 postgis-jdbc 添加到我的 maven 到我的 wildfly 中的 postgresql 的 module.xml 中,但是当我在 Hibernate Spatial 5 中阅读 THIS IS NOT NECESSARY 时。X。Wildfly 10 默认使用 5.0.7,我使用的是 hibernate 5.1.0.Final,所以我没有将 pom.xml 上任何 hibernate 组件的范围设置为“已提供”。但一切也一直失败。所以我追踪了我的图书馆。

mvn dependency:tree

您必须检查任何被调用的 postgresql 库或任何 postgis 库。我发现 Hibernate Spatial 5.1 有一些 postgresql 依赖项,所以我将它们排除在 hibernate spatial 之外。

        <exclusion>
            <artifactId>postgresql</artifactId>
            <groupId>org.postgresql</groupId>
        </exclusion>

我这样做了,我发现 PGobject 有问题,它说找不到类。所以我将它添加到 jboss-deployment-structure.xml

<?xml version="1.0" encoding="UTF-8"?>
<jboss-deployment-structure>
    <deployment>
        <dependencies>
            <module name="org.postgresql" />
        </dependencies>
    </deployment>
</jboss-deployment-structure>

这完成了工作。如果您有类似的问题,请使用 maven dependency:tree 来跟踪您的库。

于 2016-05-19T02:06:03.547 回答
2

经过几天的努力,我找到了这个解决方案:

不要通过 Wildfly 上定义的数据源连接到您的数据库。相反,在您的 persistence.xml 中:

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
<persistence-unit name="org.hibernate.events.jpa" transaction-type="JTA">
   <properties>
       <property name="hibernate.dialect" value="org.hibernate.spatial.dialect.postgis.PostgisDialect"/>
       <property name="hibernate.connection.driver_class" value="org.postgresql.Driver"/>
       <property name="hibernate.connection.url" value="jdbc:postgresql://localhost:5432/yourdatabase"/>
       <property name="hibernate.connection.username" value="username"/>
       <property name="hibernate.connection.password" value="password"/>
       <property name="hibernate.connection.pool_size" value="5"/>

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

       <property name="hibernate.max_fetch_depth" value="5"/>

       <property name="hibernate.hbm2ddl.auto" value="update"/>
   </properties>
</persistence-unit>

因为早期的 5.0.x 版本的 hibernate 显然没有正确集成 hibernate-spatial 并且为了避免类路径问题,我将文件 jboss-deployment-structure.xml 添加到我的 META-INF 中:

<?xml version="1.0" encoding="UTF-8"?>
<jboss-deployment-structure>
    <deployment>
         <exclusions>
            <module name="org.hibernate" />
            <module name="org.postgresql" />
        </exclusions>
    </deployment>
</jboss-deployment-structure>

这将阻止 Wildfly 提供的 hibernate 被您的部署使用,以便您可以改为为最新的 hibernate 版本(撰写本文时为 5.1.0)添加依赖项。然后,您需要为 hibernate、hibernate-spatial 和 postgresql-jdbc 添加依赖项。

另请注意,hibernate 5 不再需要 @Type 注释。

我已经设法让我的项目使用上面的设置和我的一个具有以下属性/列的实体:

@Column(columnDefinition = "geometry(Point,4326)")
private Point position;

我希望它有帮助,祝你好运!

编辑: 使用这种方法,您需要将您的 postgresql jdbc 驱动程序作为依赖项添加到您的项目中。

编辑:

我准备了一个工作示例项目来演示 wf10/hibernate5/postgis 的使用 - 在 github 上查看:

https://github.com/Pulvertoastmann/wf10postgis/

于 2016-03-23T13:26:21.860 回答