Following with the configuration of Postgis and Oracle spatial for different Hibernate persistence units , I will have instances of MyClass persisted into either a Postgis or an Oracle database as configured in my persistence.xml and its mapping file.
I will have a geom attribute annotated :
public abstract class MyClass (...)
@Type(type = "org.hibernatespatial.GeometryUserType")
@Column(name = "geom", columnDefinition="Geometry", nullable = true)
protected Geometry geom; (...)
Persistence.xml configured :
<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>
And in my 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 configuration will work in a similar way.
So that when I query for MyClass entities it will pick the right dialect. That seems to work and pick the right dialect for that attribute when I am inserting or retrieving info from the database (simple query.list()).
But when I am filtering on that attribute with a parameter of that type :
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();
It will use the Hibernate dialect configured by default for that particular parameter instead of the specified for the persistence unit, so it will output the stacktrace below.
Is there any way I can force to use a consistent dialect as I defined in the persistence unit for that particular parameter field in the predicate, instead of whatever is configured by default?
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)