0

我有一个在 Tomcat 8.0.20 上运行 Spring 3.1.1、Hibernate 3.6.0.Final 和 Hibernate Spatial 1.1.1 的应用程序。到目前为止,我们使用的是 Oracle 11.2.0.4,但我们想迁移到 PostgreSQL 9.2-1002.jdbc4 / PostGIS 2.1.6。

我们将在不同阶段迁移数据库模式,因此在某个阶段,我们将需要一些连接来使用 Oracle 和一些 PostGIS。为了开始开发和测试,我删除了所有 Oracle Maven 依赖项并添加了必要的依赖项以使用 Postgres,如Hibernate Spatial 1.1.1 Tutorial中所述,主要是:

<dependency>
    <groupId>org.hibernatespatial</groupId>
    <artifactId>hibernate-spatial-postgis</artifactId>
    <version>1.1.1</version>
</dependency>

在 Tomcat context.xml中,我现在已经配置了一个连接:

<Resource accessToUnderlyingConnectionAllowed="true" auth="Container"
    autoReconnect="true" driverClassName="org.postgresql.Driver"
    logAbandoned="true" maxWait="5000" minEvictableIdleTimeMillis="60000"
    name="jdbc/dbname" numTestsPerEvictionRun="3" password="xxx"
    removeAbandoned="true" removeAbandonedTimeout="60" testOnBorrow="true"
    testWhileIdle="true" timeBetweenEvictionRunsMillis="60000"
    type="javax.sql.DataSource" url="jdbc:postgresql://servername:5432/dbname"
    username="username" validationQuery="select 1" />

这工作正常。我可以先在其几何字段中使用空值将记录插入数据库,然后使用该点的坐标值对其进行更新。该字段定义为:

@Type(type = "org.hibernatespatial.GeometryUserType")
@Column(name = "geom", columnDefinition="Geometry", nullable = true) 
protected Geometry geom;

这行得通,没问题。只要我包含 Oracle 的 Maven 依赖项,问题就会出现:

<dependency>
            <artifactId>hibernate-spatial-oracle</artifactId>
            <groupId>org.hibernatespatial</groupId>
            <version>1.1.1</version>
        </dependency>

即使我没有配置任何额外的连接,也没有更改项目中的任何内容以使用 Oracle,该记录仍按预期插入 Postgres,但在更新其 Geometry 字段时,我将堆栈跟踪粘贴在下面,即即使连接保持不变,也会出现意外的“查找 Oracle 连接的问题”。

org.hibernatespatial.helper.FinderException:无法从 PreparedStatement 获取 OracleSpatial Connection 对象。

我不明白为什么 Hibernate 能够将数据插入数据库,但是在尝试更新空间数据时会寻找 Oracle 连接,只有当我添加了一个不应该使用的 Oracle Maven 依赖项时,我保持我为 PostGIS 配置的连接。

知道我可能做错了什么吗?

提前致谢。

编辑:似乎在启动 Tomcat 并初始化 GeometryUserType 时,使用 null 作为属性调用配置方法。所以它默认为一个spatialDialect,它将是它从可用中找到的第一个。由于有两个(Oracle 和 PostGIS),它只会选择第一个(恰好是 Oracle)。

所以我必须在配置中遗漏一些东西,但我认为去 * persistence.xml * 并声明持久性单元应该使用该方言就足够了。

<persistence-unit name="pu_name" transaction-type="JTA">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
        <jta-data-source>${jta.datasource.prefix}datasource_name</jta-data-source>
        <class>com.myorganization.MyClass</class>
        <exclude-unlisted-classes>true</exclude-unlisted-classes>
        <properties>
            <property name="hibernate.dialect" value="org.hibernatespatial.postgis.PostgisDialect" />
(...)

javax.persistence.PersistenceException: org.hibernate.HibernateException: Problem finding Oracle Connection
    at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1389)
    at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1317)
    at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1323)
    at org.hibernate.ejb.AbstractEntityManagerImpl.flush(AbstractEntityManagerImpl.java:965)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at org.springframework.orm.jpa.ExtendedEntityManagerCreator$ExtendedEntityManagerInvocationHandler.invoke(ExtendedEntityManagerCreator.java:365)
    at com.sun.proxy.$Proxy62.flush(Unknown Source)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at org.springframework.orm.jpa.SharedEntityManagerCreator$SharedEntityManagerInvocationHandler.invoke(SharedEntityManagerCreator.java:240)
    at com.sun.proxy.$Proxy45.flush(Unknown Source)
    at com.mapflow.geotagdata.common.processing.impl.ProcessorInnerStepsImpl$6.executeTransactionalCommand(ProcessorInnerStepsImpl.java:1056)
    at com.mapflow.geotagdata.common.processing.impl.ProcessorInnerStepsImpl$6.executeTransactionalCommand(ProcessorInnerStepsImpl.java:1)
    at com.mapflow.geotagdata.common.transactions.TransactionalCommandExecutor.executeTxRequiresNew(TransactionalCommandExecutor.java:37)
    at com.mapflow.geotagdata.common.transactions.TransactionalCommandExecutor$$FastClassByCGLIB$$7beb81b5.invoke(<generated>)
    at net.sf.cglib.proxy.MethodProxy.invoke(MethodProxy.java:191)
    at org.springframework.aop.framework.Cglib2AopProxy$CglibMethodInvocation.invokeJoinpoint(Cglib2AopProxy.java:689)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150)
    at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:110)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
    at org.springframework.aop.framework.Cglib2AopProxy$DynamicAdvisedInterceptor.intercept(Cglib2AopProxy.java:622)
    at com.mapflow.geotagdata.common.transactions.TransactionalCommandExecutor$$EnhancerByCGLIB$$4c535b1b.executeTxRequiresNew(<generated>)
    at com.mapflow.geotagdata.common.processing.impl.ProcessorInnerStepsImpl.synchPendingTaggedBatchToDB(ProcessorInnerStepsImpl.java:1012)
    at com.mapflow.geotagdata.common.processing.impl.ProcessorInnerStepsImpl.access$0(ProcessorInnerStepsImpl.java:996)
    at com.mapflow.geotagdata.common.processing.impl.ProcessorInnerStepsImpl$5.call(ProcessorInnerStepsImpl.java:969)
    at com.mapflow.geotagdata.common.processing.impl.ProcessorInnerStepsImpl$5.call(ProcessorInnerStepsImpl.java:1)
    at com.mapflow.geotagdata.common.processing.utility.RetryOnExceptionBuilder.execute(RetryOnExceptionBuilder.java:96)
    at com.mapflow.geotagdata.common.processing.impl.ProcessorInnerStepsImpl.dataTaggingPhase(ProcessorInnerStepsImpl.java:964)
    at com.mapflow.geotagdata.common.processing.impl.ProcessorOuterStepsImpl.doOuterStepLineByLineInnerSteps(ProcessorOuterStepsImpl.java:107)
    at com.mapflow.geotagdata.common.processing.impl.ProcessorImpl.process(ProcessorImpl.java:149)
    at com.mapflow.geotagdata.common.processing.impl.ProcessorImpl.processFile(ProcessorImpl.java:80)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:318)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:183)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150)
    at org.springframework.aop.interceptor.AsyncExecutionInterceptor$1.call(AsyncExecutionInterceptor.java:80)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)
Caused by: org.hibernate.HibernateException: Problem finding Oracle Connection
    at org.hibernatespatial.oracle.OracleJDBCTypeFactory.createStruct(OracleJDBCTypeFactory.java:38)
    at org.hibernatespatial.oracle.SDOGeometry.store(SDOGeometry.java:154)
    at org.hibernatespatial.oracle.SDOGeometryType.conv2DBGeometry(SDOGeometryType.java:88)
    at org.hibernatespatial.oracle.SDOGeometryType.nullSafeSet(SDOGeometryType.java:77)
    at org.hibernatespatial.GeometryUserType.nullSafeSet(GeometryUserType.java:201)
    at org.hibernate.type.CustomType.nullSafeSet(CustomType.java:140)
    at org.hibernate.persister.entity.AbstractEntityPersister.dehydrate(AbstractEntityPersister.java:2184)
    at org.hibernate.persister.entity.AbstractEntityPersister.update(AbstractEntityPersister.java:2559)
    at org.hibernate.persister.entity.AbstractEntityPersister.updateOrInsert(AbstractEntityPersister.java:2495)
    at org.hibernate.persister.entity.AbstractEntityPersister.update(AbstractEntityPersister.java:2822)
    at org.hibernate.action.EntityUpdateAction.execute(EntityUpdateAction.java:113)
    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:185)
    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.ejb.AbstractEntityManagerImpl.flush(AbstractEntityManagerImpl.java:962)
    ... 44 more
Caused by: org.hibernatespatial.helper.FinderException: Couldn't get at the OracleSpatial Connection object from the PreparedStatement.
    at org.hibernatespatial.oracle.DefaultConnectionFinder.find(DefaultConnectionFinder.java:82)
    at org.hibernatespatial.oracle.DefaultConnectionFinder.find(DefaultConnectionFinder.java:68)
    at org.hibernatespatial.oracle.DefaultConnectionFinder.find(DefaultConnectionFinder.java:68)
    at org.hibernatespatial.oracle.DefaultConnectionFinder.find(DefaultConnectionFinder.java:51)
    at org.hibernatespatial.oracle.OracleJDBCTypeFactory.createStruct(OracleJDBCTypeFactory.java:36)
    ... 61 more
4

1 回答 1

0

正如所评论的那样,在我看来,Hibernate 将默认使用 spatialDialect,这将是它从可用中找到的第一个,在这种情况下,它是 Oracle,尽管在 persistence.xml 中指定了任何内容。

我发现的第一个解决方法是在实体中注释 GeometryUserType 我将使用 Postgis 方言,如下所示:

@TypeDefs({@TypeDef(name="org.hibernatespatial.GeometryUserType", 
        parameters={@org.hibernate.annotations.Parameter(name="dialect",value="org.hibernatespatial.postgis.PostgisDialect")},
        typeClass=org.hibernatespatial.GeometryUserType.class)})

这将迫使 Hibernate 对该实体使用 Postgis。

第二种解决方法对我来说效果更好(我需要能够配置它并根据持久性单元和环境为一个实体使用不同的方言)是在持久性单元中使用映射文件。

<persistence-unit name="persistence_unit_name" transaction-type="JTA">
    <provider>org.hibernate.ejb.HibernatePersistence</provider>
    <jta-data-source>jta_data_source_name</jta-data-source>
    <mapping-file>${hibernate.mappingfile}</mapping-file>
    <class>(...)</class>

在我的 pom 文件中,我将使用 Maven 配置文件和变量将该映射文件设置为我需要的文件。

<hibernate.mappingfile>oracle.hbm.xml</hibernate.mappingfile>

或者 :

<hibernate.mappingfile>postgis.hbm.xml</hibernate.mappingfile>

所以我会有一个postgis.hbm.xml文件:

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC 
 "-//Hibernate/Hibernate Mapping DTD//EN"
 "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> 
<hibernate-mapping>
    <typedef name="org.hibernatespatial.GeometryUserType" class="org.hibernatespatial.GeometryUserType" >
        <param name="dialect">org.hibernatespatial.postgis.PostgisDialect</param>
    </typedef>
</hibernate-mapping>

还有一个oracle.hbm.xml

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC 
 "-//Hibernate/Hibernate Mapping DTD//EN"
 "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> 
<hibernate-mapping>
    <typedef name="org.hibernatespatial.GeometryUserType" class="org.hibernatespatial.GeometryUserType" >
        <param name="dialect">org.hibernatespatial.oracle.OracleSpatial10gDialect</param>
    </typedef>
</hibernate-mapping>

我想知道是否有更好的方法来做到这一点,但这些天我找不到,或者我在这里得到了回答。希望这对某人有所帮助。

于 2015-05-19T15:58:35.660 回答