小编典典

Hibernate使用PostGIS Geometry持续失败

hibernate

与上一个问题有关。我有一个使用Hibernate的Spring
Roo应用程序,它使用JTS将Geometry对象写入PostGIS数据库。我相信我已经解决了定义我的Geometry对象时遇到的问题,现在Hibernate正在执行其persist()方法,但是在它到达数据库之前就出了问题,并且在下面出现了异常。

这是一些有趣的行。首先从Hibernate日志中,要持久化的对象,然后是一个SQL查询(大概替换了?):

...
DEBUG org.hibernate.pretty.Printer - com.test.LandUse{id=1, centerPoint=POINT (5 6), version=0}
...
DEBUG org.hibernate.SQL - insert into land_use (center_point, version, id) values (?, ?, ?)
...

然后发生了更多的事情,尽管没有明显不好的事情。但是,我看不到任何“最终” SQL,并且正在尝试回滚事务。然后:

org.springframework.transaction.TransactionSystemException: Could not commit JPA transaction; nested exception is javax.persistence.RollbackException: Error while committing the transaction
    at org.springframework.orm.jpa.JpaTransactionManager.doCommit(JpaTransactionManager.java:521)
    at org.springframework.transaction.support.AbstractPlatformTransactionManager.processCommit(AbstractPlatformTransactionManager.java:754)
    at org.springframework.transaction.support.AbstractPlatformTransactionManager.commit(AbstractPlatformTransactionManager.java:723)
    at org.springframework.transaction.interceptor.TransactionAspectSupport.commitTransactionAfterReturning(TransactionAspectSupport.java:393)
    at org.springframework.transaction.aspectj.AbstractTransactionAspect.ajc$afterReturning$org_springframework_transaction_aspectj_AbstractTransactionAspect$3$2a73e96c(AbstractTransactionAspect.aj:78)
    at com.test.LandUse_Roo_Jpa_ActiveRecord.ajc$interMethod$com_test_LandUse_Roo_Jpa_ActiveRecord$com_test_LandUse$persist(LandUse_Roo_Jpa_ActiveRecord.aj:44)
    at com.test.LandUse.persist(LandUse.java:1)
    at com.test.LandUse_Roo_Jpa_ActiveRecord.ajc$interMethodDispatch1$com_test_LandUse_Roo_Jpa_ActiveRecord$com_test_LandUse$persist(LandUse_Roo_Jpa_ActiveRecord.aj)
    at com.test.LandUseController_Roo_Controller.ajc$interMethod$com_test_LandUseController_Roo_Controller$com_test_LandUseController$create(LandUseController_Roo_Controller.aj:29)
    at com.test.LandUseController.create(LandUseController.java:1)
...
Caused by: javax.persistence.RollbackException: Error while committing the transaction
at org.hibernate.ejb.TransactionImpl.commit(TransactionImpl.java:93)
    at org.springframework.orm.jpa.JpaTransactionManager.doCommit(JpaTransactionManager.java:512)
    ... 54 more
Caused by: java.lang.UnsupportedOperationException
    at org.hibernate.spatial.GeometrySqlTypeDescriptor.getBinder(GeometrySqlTypeDescriptor.java:52)
    at org.hibernate.type.AbstractStandardBasicType.nullSafeSet(AbstractStandardBasicType.java:283)
    at org.hibernate.type.AbstractStandardBasicType.nullSafeSet(AbstractStandardBasicType.java:278)
    at org.hibernate.type.AbstractSingleColumnStandardBasicType.nullSafeSet(AbstractSingleColumnStandardBasicType.java:89)
    at org.hibernate.persister.entity.AbstractEntityPersister.dehydrate(AbstractEntityPersister.java:2184)
    at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2430)
    at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2874)
    at org.hibernate.action.EntityInsertAction.execute(EntityInsertAction.java:79)
    at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:273)
    at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:265)
    at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:184)
    at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:321)
    at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:51)
    at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1216)
    at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:383)
    at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:133)
    at org.hibernate.ejb.TransactionImpl.commit(TransactionImpl.java:76)
    ... 55 more

我一直在努力使这个简单的用例(一个只有一个Geometry属性的对象)工作了一个多星期,并且已经不知所措了。如果我用String替换Geometry对象,它就可以正常工作。有谁知道是什么原因导致这种错误?

编辑:
Thierry在下面的回答让我戳了一下源代码,并且我注意到抛出了异常GeometrySqlTypeDescriptor,其中包含一些有趣的内容:

/**
 * A generic <code>SqlTypeDescriptor</code>, intended to be remapped
 * by the spatial dialect.
 *
 * @author Karel Maesen, Geovise BVBA
 *         creation-date: 7/27/11
 */
public class GeometrySqlTypeDescriptor implements SqlTypeDescriptor {

    public static final GeometrySqlTypeDescriptor INSTANCE = new GeometrySqlTypeDescriptor();

    @Override
    public int getSqlType() {
        return 3000; //this value doesn't conflict with presently defined java.sql.Types values.
    }

    @Override
    public boolean canBeRemapped() {
        return true;
    }

    @Override
    public <X> ValueBinder<X> getBinder(JavaTypeDescriptor<X> javaTypeDescriptor) {
        throw new UnsupportedOperationException();
    }

    @Override
    public <X> ValueExtractor<X> getExtractor(JavaTypeDescriptor<X> javaTypeDescriptor) {
        throw new UnsupportedOperationException();
    }
}

特别要注意的是,类注释暗示了Hibernate方言映射显然存在问题。不幸的是,我不知道这意味着什么,但是由于某种版本的不匹配,我正在猜测。(另请注意,根据我之前的错误,声明了SQL类型3000的声明!)

org.hibernate.spatial.dialect.postgis.PostgisDialect根据Hibernate
Spatial使用指南
,我当前的方言是。我正在使用Hibernate
Spatial 4.0-M1,JTS 1.12和PostGIS 2.0.1。我可能会尝试使用几种不同版本的PostGIS,尤其是因为这是Hibernate
Spatial应该提供但似乎没有提供的依赖项。


阅读 429

收藏
2020-06-20

共1个答案

小编典典

似乎问题在于未正确拾取PostgisDialect并将其正确集成,因此不支持所需的操作。解决方案就像从Hibernate
3.6.9.Final升级到4.1.6.Final一样简单!

有关更多信息,请参见邮件列表中的主题

根据该线程,您还应该知道,从Hibernate Spatial
4.0-M1开始,只有Geometry类型指定为Hibernate,因此@Column注释 必须
设置columnDefinition="Geometry",而 不是 set Point。将来可能会解决此问题。

通过这种修改,我终于可以将Point写入数据库了!正确的属性规范是:

@Column(columnDefinition="Geometry")
@Type(type = "org.hibernate.spatial.GeometryType")
private Point centerPoint;
2020-06-20