使用configuration of Postgis and Oracle spatial for different Hibernate persistence units后,我将MyClass的实例持久保存到我的 persistence.xml 及其映射文件中配置的Postgis或Oracle数据库中。
我将注释 geom 属性:
public abstract class MyClass (...)
@Type(type = "org.hibernatespatial.GeometryUserType")
@Column(name = "geom", columnDefinition="Geometry", nullable = true)
protected Geometry geom; (...)
Persistence.xml 已配置:
<persistence-unit name="pers_unit_name" transaction-type="JTA">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<jta-data-source>jta_data_source_name</jta-data-source>
<mapping-file>oracle.hbm.xml</mapping-file>
在我的 oracle.hbm.xml :
中<hibernate-mapping>
<typedef name="org.hibernatespatial.GeometryUserType" class="org.hibernatespatial.GeometryUserType" >
<param name="dialect">org.hibernatespatial.oracle.OracleSpatial10gDialect</param>
</typedef>
</hibernate-mapping>
Postgis配置将以类似的方式工作。
因此,当我查询MyClass实体时,它将选择正确的方言。当我从数据库插入或检索信息(简单的 query.list())时,这似乎可以为属性选择正确的方言。
但是当我使用该类型的参数过滤该属性时:
String queryString = "SELECT NEW map(m.id, m.geom)" +
"FROM MyClass m " +
"WHERE within (r.geom, :geometry) = true ";
final org.hibernate.Query query = session.createQuery(queryString);
query.setParameter("geometry", geom, GeometryUserType.TYPE);
return query.list();
它将使用默认为该特定参数配置的Hibernate方言而不是为持久性单元指定的方法,因此它将输出下面的堆栈跟踪。
有没有什么方法可以强制使用我在谓词中的特定参数字段的持久性单元中定义的一致方言,而不是默认配置的任何方言?
Caused by: java.sql.SQLException: Invalid column type
at oracle.jdbc.driver.OraclePreparedStatement.setObjectCritical(OraclePreparedStatement.java:8921)
at oracle.jdbc.driver.OraclePreparedStatement.setObjectInternal(OraclePreparedStatement.java:8396)
at oracle.jdbc.driver.OraclePreparedStatement.setObjectInternal(OraclePreparedStatement.java:9176)
at oracle.jdbc.driver.OraclePreparedStatement.setObject(OraclePreparedStatement.java:9153)
at oracle.jdbc.driver.OraclePreparedStatementWrapper.setObject(OraclePreparedStatementWrapper.java:234)
at org.apache.tomcat.dbcp.dbcp2.DelegatingPreparedStatement.setObject(DelegatingPreparedStatement.java:188)
at org.apache.tomcat.dbcp.dbcp2.DelegatingPreparedStatement.setObject(DelegatingPreparedStatement.java:188)
at org.hibernatespatial.AbstractDBGeometryType.nullSafeSet(AbstractDBGeometryType.java:154)
at org.hibernatespatial.GeometryUserType.nullSafeSet(GeometryUserType.java:201)
at org.hibernate.type.CustomType.nullSafeSet(CustomType.java:146)
at org.hibernate.param.NamedParameterSpecification.bind(NamedParameterSpecification.java:67)
at org.hibernate.loader.hql.QueryLoader.bindParameterValues(QueryLoader.java:578)
at org.hibernate.loader.Loader.prepareQueryStatement(Loader.java:1716)
at org.hibernate.loader.Loader.doQuery(Loader.java:801)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:274)
at org.hibernate.loader.Loader.doList(Loader.java:2542)