HHH-11764 - Fix PGGeometryTypeDescriptor

This commit is contained in:
Karel Maesen 2017-06-14 21:45:43 +02:00
parent 216e61fc9b
commit a7fc440e4c
5 changed files with 130 additions and 150 deletions

View File

@ -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;
}
};

View File

@ -16,7 +16,7 @@ import org.hibernate.type.Type;
/**
* Functions registered in all Postgis Dialects
*
* <p>
* 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();
}
}
}

View File

@ -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 <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
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<C2D> crs = CoordinateReferenceSystems.PROJECTED_2D_METER;
private final Polygon<C2D> 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<C2D> pnt = point( crs, c( 10, 5 ) );
event.setLocation( pnt );
session.persist( event );
return event.getId();
} );
doInHibernate( this::sessionFactory, session -> {
List<Event> 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<C2D> 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;
}
}
}

View File

@ -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 <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
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<Event> 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<Geometry> 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;
}
}
}

View File

@ -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