【问题标题】:Spatial Hibernate 5 On Wildfly 10Wildfly 10 上的 Spatial Hibernate 5
【发布时间】:2016-05-31 22:48:09
【问题描述】:

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

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

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

【问题讨论】:

  • 这里也一样。你在哪里可以解决这个问题?
  • 你找到解决方法了吗,我真的不想在@Toastor 建议的情况下这样做
  • 查看此答案以找到解决方案:stackoverflow.com/a/46222613/1126380,基本上您需要在运行应用程序之前在 Wildfly 中部署所有必需的库。

标签: hibernate wildfly postgis spatial hibernate-spatial


【解决方案1】:

我遇到了同样的问题,我刚刚解决了它。这基本上是一个依赖问题。问题是您在 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 中,但是当我在阅读 THIS IS NOT NECESSARY 时休眠空间 5.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 来跟踪您的库。

【讨论】:

    【解决方案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/

    【讨论】:

    • 并使用 jta-data-source?
    • 不,不需要数据源。但这里有一个有趣的地方:如果您确实创建了一个数据源,那么上面的示例可以在您的 persistence.xml 中不提供数据库凭据的情况下工作。将使用 wf ds 的凭据。我不知道为什么……显然发生了一些奇怪的事情。老实说,一旦成功,我就停止调查并转向其他事情,所以我无法为您提供任何进一步的解释......
    • @Toastor,我刚刚尝试了您的示例项目。我可以插入几何值,但无法取回它们。我在您的示例项目 github 中创建了一个问题。希望您能够帮助我。这是我的原帖:stackoverflow.com/questions/46164275/…
    猜你喜欢
    • 2015-10-05
    • 1970-01-01
    • 2015-11-13
    • 1970-01-01
    • 2016-10-02
    • 1970-01-01
    • 2018-06-08
    • 2017-03-09
    • 1970-01-01
    相关资源
    最近更新 更多