Skip to content

Commit

Permalink
HHH-13799 : Criteria API support for Hibernate Spatial (hibernate#3159)
Browse files Browse the repository at this point in the history
* HHH-13799 : Criteria API support for Hibernate Spatial

Co-authored-by: Karel Maesen <[email protected]>
  • Loading branch information
daniel-shuy and maesenka authored Feb 24, 2020
1 parent b914b02 commit cab651e
Show file tree
Hide file tree
Showing 20 changed files with 1,087 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ hibernate.dialect org.hibernate.spatial.dialect.db2.DB2SpatialDialect
hibernate.connection.driver_class com.ibm.db2.jcc.DB2Driver
hibernate.connection.url jdbc:db2://localhost:50000/hibern8
hibernate.connection.username db2inst1
hibernate.connection.password password
hibernate.connection.password oPucroAsMAgL


hibernate.connection.pool_size 5
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ hibernate.dialect org.hibernate.spatial.dialect.sqlserver.SqlServer2012SpatialDi
hibernate.connection.driver_class com.microsoft.sqlserver.jdbc.SQLServerDriver
hibernate.connection.url jdbc:sqlserver://localhost:1433;databaseName=TestDb
hibernate.connection.username hibern8
hibernate.connection.password hibern8Pass
hibernate.connection.password langpaswoord123A%1


hibernate.connection.pool_size 5
Expand Down
3 changes: 3 additions & 0 deletions hibernate-spatial/matrix-test.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
#! /bin/bash

## The same effect can be achieved by setting the system properties
# in ~/.gradle/gradle.properties

TASK=matrix
if [[ -n $@ ]]; then
TASK="$@"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,8 +164,16 @@ public enum SpatialFunction {
/**
* the extents function
*/
extent( "common" );
extent( "common" ),

/**
* The filter function
* <p>
* <p>Corresponds to the Oracle Spatial's "SDO_FILTER" function, or the "&&" operator of PostGIS.
*/
filter( "filter" ),

;

private final String description;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
package org.hibernate.spatial.dialect;

/**
* An Interface for {@code SpatialDialect}s that require a custom
* rendering to JPAQL for the filter predicate
* <p>
* Created by Karel Maesen, Geovise BVBA on 09/02/2020.
*/
public interface WithCustomJPAFilter {

String filterExpression(String geometryParam, String filterParam);
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,15 @@

package org.hibernate.spatial.dialect.h2geodb;

import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import org.hibernate.QueryException;
import org.hibernate.boot.model.TypeContributions;
import org.hibernate.dialect.H2Dialect;
import org.hibernate.dialect.function.StandardSQLFunction;
import org.hibernate.engine.spi.SessionFactoryImplementor;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.spatial.GeolatteGeometryJavaTypeDescriptor;
import org.hibernate.spatial.GeolatteGeometryType;
Expand All @@ -19,6 +25,7 @@
import org.hibernate.spatial.SpatialFunction;
import org.hibernate.spatial.SpatialRelation;
import org.hibernate.type.StandardBasicTypes;
import org.hibernate.type.Type;

/**
* Extends the H2Dialect by also including information on spatial functions.
Expand Down Expand Up @@ -74,6 +81,8 @@ public GeoDBDialect() {

registerFunction( "dwithin", new StandardSQLFunction( "ST_DWithin", StandardBasicTypes.BOOLEAN ) );

// Register Spatial Filter function
registerFunction( SpatialFunction.filter.name(), new FilterFunction() );
}

@Override
Expand Down Expand Up @@ -157,4 +166,26 @@ public boolean supports(SpatialFunction function) {
return function != SpatialFunction.difference && ( getFunctions().get( function.toString() ) != null );
}

private static class FilterFunction extends StandardSQLFunction {

public FilterFunction() {
super( "&&" );
}

@Override
public String render(
Type firstArgumentType, List arguments, SessionFactoryImplementor sessionFactory) {
int argumentCount = arguments.size();
if ( argumentCount != 2 ) {
throw new QueryException( String.format( "2 arguments expected, received %d", argumentCount ) );
}

return Stream.of(
String.valueOf( arguments.get( 0 ) ),
getRenderedName( arguments ),
String.valueOf( arguments.get( 1 ) )
).collect( Collectors.joining( " ", "(", ")" ) );
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@
package org.hibernate.spatial.dialect.hana;

import java.sql.Types;
import java.util.List;

import org.hibernate.boot.model.TypeContributions;
import org.hibernate.dialect.HANAColumnStoreDialect;
import org.hibernate.engine.config.spi.ConfigurationService;
import org.hibernate.engine.config.spi.ConfigurationService.Converter;
import org.hibernate.engine.spi.SessionFactoryImplementor;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.spatial.GeolatteGeometryJavaTypeDescriptor;
import org.hibernate.spatial.GeolatteGeometryType;
Expand All @@ -22,6 +24,7 @@
import org.hibernate.spatial.SpatialFunction;
import org.hibernate.spatial.SpatialRelation;
import org.hibernate.type.StandardBasicTypes;
import org.hibernate.type.Type;

public class HANASpatialDialect extends HANAColumnStoreDialect implements SpatialDialect {

Expand Down Expand Up @@ -102,6 +105,9 @@ public HANASpatialDialect() {
registerFunction(
SpatialFunction.within.name(),
new HANASpatialFunction( "ST_Within", StandardBasicTypes.NUMERIC_BOOLEAN, true ) );
registerFunction(
SpatialFunction.filter.name(),
new FilterFunction() );

/*
* Additional HANA functions
Expand Down Expand Up @@ -408,4 +414,17 @@ public boolean supports(SpatialFunction function) {
}
return false;
}

private static class FilterFunction extends HANASpatialFunction {

public FilterFunction() {
super( "ST_IntersectsFilter", StandardBasicTypes.NUMERIC_BOOLEAN, true );
}

@Override
public String render(
Type firstArgumentType, List arguments, SessionFactoryImplementor sessionFactory) {
return super.render( firstArgumentType, arguments, sessionFactory ) + " = 1";
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
package org.hibernate.spatial.dialect.mysql;

import org.hibernate.dialect.function.StandardSQLFunction;
import org.hibernate.spatial.SpatialFunction;
import org.hibernate.spatial.dialect.SpatialFunctionsRegistry;
import org.hibernate.type.StandardBasicTypes;

Expand Down Expand Up @@ -163,6 +164,13 @@ class MySQL5SpatialFunctions extends SpatialFunctionsRegistry {
// "union"
// )
// );

functionMap.put(
SpatialFunction.filter.name(), new StandardSQLFunction(
"MBRIntersects",
StandardBasicTypes.BOOLEAN
)
);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
package org.hibernate.spatial.dialect.mysql;

import org.hibernate.dialect.function.StandardSQLFunction;
import org.hibernate.spatial.SpatialFunction;
import org.hibernate.spatial.dialect.SpatialFunctionsRegistry;
import org.hibernate.type.StandardBasicTypes;

Expand Down Expand Up @@ -169,6 +170,13 @@ class MySQL8SpatialFunctions extends SpatialFunctionsRegistry {
"ST_Union"
)
);

functionMap.put(
SpatialFunction.filter.name(), new StandardSQLFunction(
"MBRIntersects",
StandardBasicTypes.BOOLEAN
)
);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
package org.hibernate.spatial.dialect.oracle;

import java.io.Serializable;
import java.util.Locale;

import org.hibernate.boot.model.TypeContributions;
import org.hibernate.boot.registry.selector.spi.StrategySelector;
Expand All @@ -22,6 +23,7 @@
import org.hibernate.spatial.SpatialFunction;
import org.hibernate.spatial.SpatialRelation;
import org.hibernate.spatial.dialect.SpatialFunctionsRegistry;
import org.hibernate.spatial.dialect.WithCustomJPAFilter;

import org.jboss.logging.Logger;

Expand All @@ -33,7 +35,7 @@
* <p>
* Created by Karel Maesen, Geovise BVBA on 01/11/16.
*/
class OracleSDOSupport implements SpatialDialect, Serializable {
class OracleSDOSupport implements SpatialDialect, Serializable, WithCustomJPAFilter {

private static final HSMessageLogger log = Logger.getMessageLogger(
HSMessageLogger.class,
Expand Down Expand Up @@ -290,7 +292,7 @@ public String getDWithinSQL(String columnName) {
*/
@Override
public String getHavingSridSQL(String columnName) {
return String.format( " (MDSYS.ST_GEOMETRY(%s).ST_SRID() = ?)", columnName );
return String.format( " (MDSYS.ST_GEOMETRY(%s).ST_SRID() = ?)", columnName , Locale.US);
}

/**
Expand All @@ -304,7 +306,7 @@ public String getHavingSridSQL(String columnName) {
*/
@Override
public String getIsEmptySQL(String columnName, boolean isEmpty) {
return String.format( "( MDSYS.ST_GEOMETRY(%s).ST_ISEMPTY() = %d )", columnName, isEmpty ? 1 : 0 );
return String.format( "( MDSYS.ST_GEOMETRY(%s).ST_ISEMPTY() = %d )", columnName, isEmpty ? 1 : 0 , Locale.US);
}

/**
Expand All @@ -331,4 +333,8 @@ public boolean supports(SpatialFunction function) {
}


@Override
public String filterExpression(String geometryParam, String filterParam) {
return SpatialFunction.filter.name() + "(" + geometryParam + ", " + filterParam + ") = 'TRUE' ";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import org.hibernate.spatial.HSMessageLogger;
import org.hibernate.spatial.SpatialDialect;
import org.hibernate.spatial.SpatialFunction;
import org.hibernate.spatial.dialect.WithCustomJPAFilter;

import org.jboss.logging.Logger;

Expand All @@ -26,7 +27,7 @@
*
* @author Karel Maesen
*/
public class OracleSpatial10gDialect extends Oracle10gDialect implements SpatialDialect, Serializable {
public class OracleSpatial10gDialect extends Oracle10gDialect implements SpatialDialect, WithCustomJPAFilter, Serializable {

private static final HSMessageLogger log = Logger.getMessageLogger(
HSMessageLogger.class,
Expand Down Expand Up @@ -101,5 +102,8 @@ public boolean supports(SpatialFunction function) {
return ( getFunctions().get( function.toString() ) != null );
}


@Override
public String filterExpression(String geometryParam, String filterParam) {
return sdoSupport.filterExpression( geometryParam, filterParam );
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import org.hibernate.dialect.function.StandardSQLFunction;
import org.hibernate.engine.spi.SessionFactoryImplementor;
import org.hibernate.spatial.SpatialAnalysis;
import org.hibernate.spatial.SpatialFunction;
import org.hibernate.spatial.SpatialRelation;
import org.hibernate.spatial.dialect.SpatialFunctionsRegistry;
import org.hibernate.spatial.dialect.oracle.criterion.OracleSpatialAggregate;
Expand Down Expand Up @@ -138,6 +139,12 @@ class OracleSpatialFunctions extends SpatialFunctionsRegistry {
new SpatialAggregationFunction( "extent", OracleSpatialAggregate.EXTENT, sdoSupport )
);

// spatial filter function
put(
SpatialFunction.filter.name(),
new StandardSQLFunction( "SDO_FILTER" )
);

//other common functions

put( "transform", new StandardSQLFunction( "SDO_CS.TRANSFORM" ) );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import org.hibernate.spatial.HSMessageLogger;
import org.hibernate.spatial.SpatialDialect;
import org.hibernate.spatial.SpatialFunction;
import org.hibernate.spatial.dialect.WithCustomJPAFilter;

import org.jboss.logging.Logger;

Expand All @@ -25,7 +26,8 @@
* <p>
* Created by Karel Maesen, Geovise BVBA on 11/02/17.
*/
public class OracleSpatialSDO10gDialect extends Oracle10gDialect implements SpatialDialect, Serializable {
public class OracleSpatialSDO10gDialect extends Oracle10gDialect
implements SpatialDialect, WithCustomJPAFilter, Serializable {

private static final HSMessageLogger log = Logger.getMessageLogger(
HSMessageLogger.class,
Expand Down Expand Up @@ -100,5 +102,8 @@ public boolean supports(SpatialFunction function) {
return !function.equals( SpatialFunction.crosses ) && ( getFunctions().get( function.toString() ) != null );
}


@Override
public String filterExpression(String geometryParam, String filterParam) {
return sdoSupport.filterExpression( geometryParam, filterParam );
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,13 @@
package org.hibernate.spatial.dialect.postgis;

import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import org.hibernate.QueryException;
import org.hibernate.dialect.function.StandardSQLFunction;
import org.hibernate.engine.spi.SessionFactoryImplementor;
import org.hibernate.spatial.SpatialFunction;
import org.hibernate.spatial.dialect.SpatialFunctionsRegistry;
import org.hibernate.type.StandardBasicTypes;
import org.hibernate.type.Type;
Expand Down Expand Up @@ -177,6 +181,11 @@ class PostgisFunctions extends SpatialFunctionsRegistry {
"extent", new ExtentFunction()
);

//register Spatial Filter function
put(
SpatialFunction.filter.name(), new FilterFunction()
);

//other common functions
put(
"dwithin", new StandardSQLFunction(
Expand Down Expand Up @@ -206,4 +215,26 @@ public String render(
}
}

private static class FilterFunction extends StandardSQLFunction {

public FilterFunction() {
super( "&&" );
}

@Override
public String render(
Type firstArgumentType, List arguments, SessionFactoryImplementor sessionFactory) {
int argumentCount = arguments.size();
if ( argumentCount != 2 ) {
throw new QueryException( String.format( "2 arguments expected, received %d", argumentCount ) );
}

return Stream.of(
String.valueOf( arguments.get( 0 ) ),
getRenderedName( arguments ),
String.valueOf( arguments.get( 1 ) )
).collect( Collectors.joining( " ", "(", ")" ) );
}
}

}
Loading

0 comments on commit cab651e

Please sign in to comment.