Couldn't get at the OracleSpatial Connection object from the PreparedStatement

依然范特西╮ 提交于 2019-12-11 03:49:19

问题


i am using hibernate spatial 4.3.2 along with hibernate 4.3.11 and my db is oracle 12c. I use hikariCP for connection pool. when I use hikacriCP I get an error that seems that cause is hibernate spatial can not access to the wrapped connection of hikari connection instance, this is my exception: problem is hibernate throws error when saving an entity that contains geometry field.

Couldn't get at the OracleSpatial Connection object from the PreparedStatement

when I remove hikari, everything is fine! here is my entity and xml mapping file.

<hibernate-mapping>
    <class   name="MyPoint"  table="MyPoint"  >
        <id name="id" type="long" >
            <column name="ID"  />
            <generator class="sequence" >
                <param name="sequence">SEQ_MyPoint</param>
            </generator>
        </id>
        <property           name="name"             column="name"               type="string"                                           not-null="true" />
        <property           name="geom"             column="geom"               type="org.hibernate.spatial.GeometryType"               not-null="true" />

    </class>
</hibernate-mapping>


public class MyPoint extends BaseEntity<Long> {
    private String name;
    private Point geom;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Point getGeom() {
        return geom;
    }

    public void setGeom(Point geom) {
        this.geom = geom;
    }
}

and here is code to save entity:

 Geometry geom = wktToGeometry(wkt);
 geom.setSRID(4326);
 MyPoint p= new  MyPoint();
 p.setName("some one");
 p.setGeom((Point) geom);
 repository.save(p);

hibernate spatial config in yml:

 jpa:
    open-in-view: false
    hibernate:
      ddl-auto: update
      naming:
        strategy: org.hibernate.cfg.ImprovedNamingStrategy
    properties:
      hibernate:
        dialect: org.hibernate.spatial.dialect.oracle.OracleSpatial10gDialect
        default_schema: my_schema
        show_sql: true
        current_session_context_class: org.springframework.orm.hibernate4.SpringSessionContext
        spatial:
           ogc_strict: true
           connection_finder: org.hibernate.spatial.dialect.oracle.DefaultConnectionFinder

here is my stacktrace:

Caused by: org.hibernate.HibernateException: Problem finding Oracle Connection
    at org.hibernate.spatial.dialect.oracle.OracleJDBCTypeFactory.createStruct(OracleJDBCTypeFactory.java:123)
    at org.hibernate.spatial.dialect.oracle.SDOGeometryValueBinder.store(SDOGeometryValueBinder.java:76)
    at org.hibernate.spatial.dialect.oracle.SDOGeometryValueBinder.toNative(SDOGeometryValueBinder.java:83)
    at org.hibernate.spatial.dialect.oracle.SDOGeometryValueBinder.bind(SDOGeometryValueBinder.java:66)
    at org.hibernate.spatial.dialect.oracle.SDOGeometryValueBinder.bind(SDOGeometryValueBinder.java:52)
    at org.hibernate.type.AbstractStandardBasicType.nullSafeSet(AbstractStandardBasicType.java:286)
    at org.hibernate.type.AbstractStandardBasicType.nullSafeSet(AbstractStandardBasicType.java:281)
    at org.hibernate.type.AbstractSingleColumnStandardBasicType.nullSafeSet(AbstractSingleColumnStandardBasicType.java:56)
    at org.hibernate.persister.entity.AbstractEntityPersister.dehydrate(AbstractEntityPersister.java:2843)
    at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:3121)
    at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:3581)
    at org.hibernate.action.internal.EntityInsertAction.execute(EntityInsertAction.java:104)
    at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:465)
    at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:351)
    at org.hibernate.event.internal.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:350)
    at org.hibernate.event.internal.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:56)
    at org.hibernate.internal.SessionImpl.flush(SessionImpl.java:1258)
    at org.hibernate.internal.SessionImpl.managedFlush(SessionImpl.java:425)
    at org.hibernate.engine.transaction.internal.jdbc.JdbcTransaction.beforeTransactionCommit(JdbcTransaction.java:101)
    at org.hibernate.engine.transaction.spi.AbstractTransactionImpl.commit(AbstractTransactionImpl.java:177)
    at org.springframework.orm.hibernate4.HibernateTransactionManager.doCommit(HibernateTransactionManager.java:584)
    ... 138 more
Caused by: org.hibernate.spatial.helper.FinderException: Couldn't get at the OracleSpatial Connection object from the PreparedStatement.
    at org.hibernate.spatial.dialect.oracle.DefaultConnectionFinder.find(DefaultConnectionFinder.java:103)
    at org.hibernate.spatial.dialect.oracle.DefaultConnectionFinder.find(DefaultConnectionFinder.java:44)
    at org.hibernate.spatial.dialect.oracle.OracleJDBCTypeFactory.createStruct(OracleJDBCTypeFactory.java:121)
    ... 158 more

2017-12-11 16:10:00 INFO  o.h.e.j.b.internal.AbstractBatchImpl - HHH000010: On release of batch it still contained JDBC statements

回答1:


I had the same issue and solved it by providing my own implementation of Connection finder interface and configuring it in the hibernate.spatial.connection_finder property.

To get an oracle.jdbc.driver.OracleConnection from Hikari you can use:

((HikariProxyConnection) connection).unwrap(OracleConnection.class);



回答2:


I finally solved the problem, by extending DefaultConnectionFinder and overriding find method:

 @Override
    public Connection find(Connection con) {
        Field delegate = ((HikariProxyConnection) con).getClass().getSuperclass().getDeclaredField("delegate");
        delegate.setAccessible(true);
        return (Connection) delegate.get(con);
    }


来源:https://stackoverflow.com/questions/47753350/couldnt-get-at-the-oraclespatial-connection-object-from-the-preparedstatement

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!