【发布时间】:2014-05-24 01:54:26
【问题描述】:
我正在尝试在 OSGi 场景中使用 Hibernate-Spatial,但我很难让它发挥作用。
我正在使用 JBoss Fuse/Fabric8。所以我的东西在 Karaf (2.3.x) 中运行,带有容器管理的事务。
数据库是 PostgresDB (9.3),PostGIS 在 Docker 中运行。
问题:
我的问题是从数据库中检索几何数据类型。我可以持久化实体,但在检索空间数据时总是出现以下错误。
java.lang.IllegalArgumentException: Can't convert object of type org.postgresql.util.PGobject
at org.hibernate.spatial.dialect.postgis.PGGeometryValueExtractor.toJTS(PGGeometryValueExtractor.java:99)
上下文:
容器中加载的捆绑包:
hibernate-commons-annotations (4.0.4.Final)
hibernate-core (4.2.10.Final)
hibernate-entitymanager (4.2.10.Final)
hibernate-osgi (4.2.10.Final)
JTS Topology Suite (1.12)
Hibernate Spatial (4.0)
PostGIS JDBC driver (1.5.8)
PostgreSQL JDBC4 driver (9.1.902.jdbc4)
实体:
...
@Column(nullable = false, unique = true)
@Type(type = "org.hibernate.spatial.GeometryType")
private Point location;
...
数据源:
<bean
id="data_source_postgres" class="org.postgresql.ds.PGPoolingDataSource">
<property name="serverName" value="serverName" />
<property name="portNumber" value="portNumber" />
<property name="databaseName" value="databaseName" />
<property name="user" value="user" />
<property name="password" value="password" />
</bean>
<service ref="data_source_postgres" interface="javax.sql.DataSource">
<service-properties>
<entry key="osgi.jndi.service.name" value="jdbc/postgresds" />
<entry key="service.exported.interfaces" value="*" />
</service-properties>
</service>
persistence.xml:
<persistence-unit name="PU" transaction-type="JTA">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<jta-data-source>osgi:service/javax.sql.DataSource/(osgi.jndi.service.name=jdbc/postgresds)</jta-data-source>
<class>entities.FooEntity</class>
<exclude-unlisted-classes>true</exclude-unlisted-classes>
<properties>
<property name="hibernate.dialect" value="org.hibernate.spatial.dialect.postgis.PostgisDialect"/>
<property name="hibernate.archive.autodetection" value="class, hbm" />
<property name="hibernate.hbm2ddl.auto" value="create" />
<property name="hibernate.show_sql" value="true" />
<property name="hibernate.format_sql" value="true" />
</properties>
</persistence-unit>
事实、结论和问题:
当我在 Karaf 之外针对同一个数据库运行代码时,具有相同的依赖项但使用手动管理的事务,它工作正常。 所以它让我得出结论,问题不是任何依赖项之间的不兼容。如果是这样的话,在卡拉夫外面跑就不行了。
我已经检查了org.hibernate.spatial.dialect.postgis.PGGeometryValueExtractor.toJTS 的实现,并得出结论认为检索到的对象类型根本不应该是 PGobject!在我的情况下,它应该是一个 org.postgis.Geometry.POINT
从其他人那里读到类似的帖子我认为适当的数据类型不会被正确注册。但如果是这样的话,它是否可以成功地坚持 Geometry 并且未能检索到它?如何检查哪些数据类型实际上已注册?
问题的根源是什么?是容器管理事务(Aires)吗?捆绑包是否没有导出所有应有的内容?可能是依赖版本吗?我是否使用了错误的方言/驱动程序/数据源?
【问题讨论】:
标签: hibernate postgresql osgi postgis apache-karaf