diff --git a/hibernate-spatial/src/main/java/org/hibernate/spatial/dialect/postgis/PGGeometryTypeDescriptor.java b/hibernate-spatial/src/main/java/org/hibernate/spatial/dialect/postgis/PGGeometryTypeDescriptor.java index 34602eb884..9457198169 100644 --- a/hibernate-spatial/src/main/java/org/hibernate/spatial/dialect/postgis/PGGeometryTypeDescriptor.java +++ b/hibernate-spatial/src/main/java/org/hibernate/spatial/dialect/postgis/PGGeometryTypeDescriptor.java @@ -60,19 +60,25 @@ public class PGGeometryTypeDescriptor implements SqlTypeDescriptor { @Override protected void doBind(PreparedStatement st, X value, int index, WrapperOptions options) throws SQLException { - final WkbEncoder encoder = Wkb.newEncoder( Wkb.Dialect.POSTGIS_EWKB_1 ); - final Geometry geometry = getJavaDescriptor().unwrap( value, Geometry.class, options ); - final byte[] bytes = encoder.encode( geometry, ByteOrder.NDR ).toByteArray(); - st.setBytes( index, bytes ); + final PGobject obj = toPGobject( value, options ); + st.setObject( index, obj ); } @Override protected void doBind(CallableStatement st, X value, String name, WrapperOptions options) throws SQLException { + final PGobject obj = toPGobject( value, options ); + st.setObject( name, obj ); + } + + private PGobject toPGobject(X value, WrapperOptions options) throws SQLException { final WkbEncoder encoder = Wkb.newEncoder( Wkb.Dialect.POSTGIS_EWKB_1 ); final Geometry geometry = getJavaDescriptor().unwrap( value, Geometry.class, options ); - final byte[] bytes = encoder.encode( geometry, ByteOrder.NDR ).toByteArray(); - st.setBytes( name, bytes ); + final String hexString = encoder.encode( geometry, ByteOrder.NDR ).toString(); + final PGobject obj = new PGobject(); + obj.setType( "geometry" ); + obj.setValue( hexString ); + return obj; } }; diff --git a/hibernate-spatial/src/main/java/org/hibernate/spatial/dialect/postgis/PostgisFunctions.java b/hibernate-spatial/src/main/java/org/hibernate/spatial/dialect/postgis/PostgisFunctions.java index c128d90503..959151fd08 100644 --- a/hibernate-spatial/src/main/java/org/hibernate/spatial/dialect/postgis/PostgisFunctions.java +++ b/hibernate-spatial/src/main/java/org/hibernate/spatial/dialect/postgis/PostgisFunctions.java @@ -16,7 +16,7 @@ import org.hibernate.type.Type; /** * Functions registered in all Postgis Dialects - * + *

* Created by Karel Maesen, Geovise BVBA on 29/10/16. */ class PostgisFunctions extends SpatialFunctionsRegistry { @@ -139,7 +139,8 @@ class PostgisFunctions extends SpatialFunctionsRegistry { ) ); put( - "buffer", new BufferFunction() + "buffer", new StandardSQLFunction( "st_buffer" + ) ); put( "convexhull", new StandardSQLFunction( @@ -200,28 +201,4 @@ class PostgisFunctions extends SpatialFunctionsRegistry { } } - private static class BufferFunction extends StandardSQLFunction { - - public BufferFunction() { - super( "st_buffer" ); - } - - @Override - public String render( - Type firstArgumentType, List arguments, SessionFactoryImplementor sessionFactory) { - final StringBuilder buf = new StringBuilder(); - buf.append( getName() ).append( '(' ); - for ( int i = 0; i < arguments.size(); i++ ) { - buf.append( arguments.get( i ) ); - if( i == 0 ) { - buf.append( "::geometry" ); - } - if ( i < arguments.size() - 1 ) { - buf.append( ", " ); - } - } - return buf.append( ")::geometry" ).toString(); - } - } - } diff --git a/hibernate-spatial/src/test/java/org/hibernate/spatial/integration/geolatte/PostgisBufferTest.java b/hibernate-spatial/src/test/java/org/hibernate/spatial/integration/geolatte/PostgisBufferTest.java new file mode 100644 index 0000000000..50fbcb615b --- /dev/null +++ b/hibernate-spatial/src/test/java/org/hibernate/spatial/integration/geolatte/PostgisBufferTest.java @@ -0,0 +1,112 @@ +/* + * 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 . + */ +package org.hibernate.spatial.integration.geolatte; + +import javax.persistence.Entity; +import javax.persistence.Id; +import java.util.List; + +import org.geolatte.geom.C2D; +import org.geolatte.geom.Point; +import org.geolatte.geom.Polygon; +import org.geolatte.geom.crs.CoordinateReferenceSystem; +import org.geolatte.geom.crs.CoordinateReferenceSystems; + +import org.hibernate.spatial.dialect.postgis.PostgisPG95Dialect; + +import org.junit.Test; + +import org.hibernate.testing.RequiresDialect; +import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; + +import static org.geolatte.geom.builder.DSL.c; +import static org.geolatte.geom.builder.DSL.point; +import static org.geolatte.geom.builder.DSL.polygon; +import static org.geolatte.geom.builder.DSL.ring; +import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate; +import static org.junit.Assert.assertEquals; + +/** + * @author Vlad Mihalcea, Karel Maesen + */ +@RequiresDialect(PostgisPG95Dialect.class) +public class PostgisBufferTest extends BaseCoreFunctionalTestCase { + + public static CoordinateReferenceSystem crs = CoordinateReferenceSystems.PROJECTED_2D_METER; + + private final Polygon window = polygon( crs, ring( c( 1, 1 ), c( 1, 20 ), + c( 20, 20 ), c( 20, 1 ), c( 1, 1 ) + ) ); + + + @Override + protected Class[] getAnnotatedClasses() { + return new Class[] { + Event.class, + }; + } + + @Test + public void test() { + Long addressId = doInHibernate( this::sessionFactory, session -> { + Event event = new Event(); + event.setId( 1L ); + event.setName( "Hibernate ORM presentation" ); + Point pnt = point( crs, c( 10, 5 ) ); + event.setLocation( pnt ); + session.persist( event ); + return event.getId(); + } ); + + doInHibernate( this::sessionFactory, session -> { + List events = session.createQuery( + "select e " + + "from Event e " + + "where buffer(:window, 100) is not null", Event.class ) + .setParameter( "window", window ) + .getResultList(); + + assertEquals( 1, events.size() ); + + } ); + } + + @Entity(name = "Event") + public static class Event { + + @Id + private Long id; + + private String name; + + private Point location; + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public Point getLocation() { + return location; + } + + public void setLocation(Point location) { + this.location = location; + } + } +} diff --git a/hibernate-spatial/src/test/java/org/hibernate/spatial/testing/dialects/postgis/PostgisBufferTest.java b/hibernate-spatial/src/test/java/org/hibernate/spatial/testing/dialects/postgis/PostgisBufferTest.java deleted file mode 100644 index cb16562253..0000000000 --- a/hibernate-spatial/src/test/java/org/hibernate/spatial/testing/dialects/postgis/PostgisBufferTest.java +++ /dev/null @@ -1,115 +0,0 @@ -/* - * 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 . - */ -package org.hibernate.spatial.testing.dialects.postgis; - -import java.util.List; -import javax.persistence.Entity; -import javax.persistence.Id; - -import org.hibernate.spatial.dialect.postgis.PostgisPG95Dialect; - -import org.hibernate.testing.RequiresDialect; -import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; -import org.junit.Test; - -import com.vividsolutions.jts.geom.Coordinate; -import com.vividsolutions.jts.geom.Geometry; -import com.vividsolutions.jts.geom.GeometryFactory; -import com.vividsolutions.jts.geom.Point; -import com.vividsolutions.jts.geom.Polygon; - -import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate; -import static org.junit.Assert.assertEquals; - -/** - * @author Vlad Mihalcea - */ -@RequiresDialect(PostgisPG95Dialect.class) -public class PostgisBufferTest extends BaseCoreFunctionalTestCase { - - private GeometryFactory geometryFactory = new GeometryFactory(); - - @Override - protected Class[] getAnnotatedClasses() { - return new Class[] { - Event.class, - }; - } - - @Test - public void test() { - Long addressId = doInHibernate( this::sessionFactory, session -> { - Event event = new Event(); - event.setId( 1L); - event.setName( "Hibernate ORM presentation"); - Point point = geometryFactory.createPoint( new Coordinate( 10, 5 ) ); - event.setLocation( point ); - - session.persist( event ); - return event.getId(); - }); - - doInHibernate( this::sessionFactory, session -> { - Coordinate [] coordinates = new Coordinate[] { - new Coordinate(1,1), new Coordinate(20,1), new Coordinate(20,20), - new Coordinate(1,20), new Coordinate(1,1) - }; - Polygon window = geometryFactory.createPolygon( coordinates ); - - List events = session.createQuery( - "select e " + - "from Event e " + - "where buffer(:window, 100) is not null", Event.class) - .setParameter("window", window) - .getResultList(); - - assertEquals(1, events.size()); - - List locations = session.createQuery( - "select buffer(e.location, 10) " + - "from Event e ", Geometry.class) - .getResultList(); - - assertEquals(1, locations.size()); - }); - } - -@Entity(name = "Event") -public static class Event { - - @Id - private Long id; - - private String name; - - private Point location; - - public Long getId() { - return id; - } - - public void setId(Long id) { - this.id = id; - } - - public String getName() { - return name; - } - - public void setName(String name) { - this.name = name; - } - - public Point getLocation() { - return location; - } - - public void setLocation(Point location) { - this.location = location; - } -} -} diff --git a/hibernate-spatial/src/test/resources/hibernate.properties b/hibernate-spatial/src/test/resources/hibernate.properties index b8af70869a..8a33ab50ac 100644 --- a/hibernate-spatial/src/test/resources/hibernate.properties +++ b/hibernate-spatial/src/test/resources/hibernate.properties @@ -28,9 +28,9 @@ hibernate.connection.password @jdbc.pass@ ## Configs for spatial databases (used during testing on local dev environment). # -#hibernate.dialect org.hibernate.spatial.dialect.postgis.PostgisDialect +#hibernate.dialect org.hibernate.spatial.dialect.postgis.PostgisPG95Dialect #hibernate.connection.driver_class org.postgresql.Driver -#hibernate.connection.url jdbc:postgresql://localhost:5432:hibbrtru +#hibernate.connection.url jdbc:postgresql://localhost:5432/hibbrtru #hibernate.connection.username hibbrtru #hibernate.connection.password hibbrtru @@ -84,7 +84,7 @@ hibernate.connection.password @jdbc.pass@ ## ## MySQL 5 InnoDDB dialect ## -#hibernate.dialect org.hibernate.spatial.dialect.mysql.MySQLSpatial5InnoDBDialect +#hibernate.dialect org.hibernate.spatial.dialect.mysql.MySQL56SpatialDialect #hibernate.connection.driver_class com.mysql.jdbc.Driver #hibernate.connection.url jdbc:mysql://localhost/testhbs #hibernate.connection.username hibernate