HHH-14932 Use the correct WKB Dialect

This commit is contained in:
Karel Maesen 2022-02-02 20:53:54 +01:00
parent d569ada1f7
commit 8907bf7d8a
10 changed files with 114 additions and 35 deletions

View File

@ -53,10 +53,11 @@ public class PGGeometryTypeDescriptor implements SqlTypeDescriptor {
if ( object == null ) {
return null;
}
ByteBuffer buffer = null;
ByteBuffer buffer;
if ( object instanceof PGobject ) {
String pgValue = ( (PGobject) object ).getValue();
assert pgValue != null;
if ( pgValue.startsWith( "00" ) || pgValue.startsWith( "01" ) ) {
//we have a WKB because this pgValue starts with the bit-order byte
buffer = ByteBuffer.from( pgValue );
@ -91,7 +92,7 @@ public class PGGeometryTypeDescriptor implements SqlTypeDescriptor {
return new ValueBinder<X>() {
@Override
public final void bind(PreparedStatement st, X value, int index, WrapperOptions options)
public void bind(PreparedStatement st, X value, int index, WrapperOptions options)
throws SQLException {
if ( value == null ) {
st.setNull( index, Types.OTHER );
@ -102,7 +103,7 @@ public class PGGeometryTypeDescriptor implements SqlTypeDescriptor {
}
@Override
public final void bind(CallableStatement st, X value, String name, WrapperOptions options)
public void bind(CallableStatement st, X value, String name, WrapperOptions options)
throws SQLException {
if ( value == null ) {
st.setNull( name, Types.OTHER );
@ -112,21 +113,21 @@ public class PGGeometryTypeDescriptor implements SqlTypeDescriptor {
}
}
protected void doBind(PreparedStatement st, X value, int index, WrapperOptions options)
private void doBind(PreparedStatement st, X value, int index, WrapperOptions options)
throws SQLException {
final PGobject obj = toPGobject( value, options );
st.setObject( index, obj );
}
protected void doBind(CallableStatement st, X value, String name, WrapperOptions options)
private 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 = javaTypeDescriptor.unwrap( value, Geometry.class, options );
final WkbEncoder encoder = Wkb.newEncoder( wkbDialect );
final Geometry<?> geometry = javaTypeDescriptor.unwrap( value, Geometry.class, options );
final String hexString = encoder.encode( geometry, ByteOrder.NDR ).toString();
final PGobject obj = new PGobject();
obj.setType( "geometry" );

View File

@ -23,7 +23,7 @@ import org.hibernate.spatial.SpatialFunction;
public class PostgisPG82Dialect extends PostgreSQL82Dialect implements SpatialDialect {
transient private PostgisSupport support = new PostgisSupport();
final transient private PostgisSupport support = new PostgisSupport();
/**
* Creates an instance
@ -45,7 +45,7 @@ public class PostgisPG82Dialect extends PostgreSQL82Dialect implements SpatialDi
typeContributions,
serviceRegistry
);
support.contributeTypes( typeContributions, serviceRegistry );
support.contributeTypes( typeContributions, serviceRegistry, PGGeometryTypeDescriptor.INSTANCE_WKB_1 );
}
@Override

View File

@ -23,7 +23,7 @@ import org.hibernate.spatial.SpatialFunction;
public class PostgisPG91Dialect extends PostgreSQL91Dialect implements SpatialDialect {
transient private PostgisSupport support = new PostgisSupport();
final transient private PostgisSupport support = new PostgisSupport();
/**
* Creates an instance
@ -45,7 +45,7 @@ public class PostgisPG91Dialect extends PostgreSQL91Dialect implements SpatialDi
typeContributions,
serviceRegistry
);
support.contributeTypes( typeContributions, serviceRegistry );
support.contributeTypes( typeContributions, serviceRegistry, PGGeometryTypeDescriptor.INSTANCE_WKB_1 );
}
@Override

View File

@ -23,7 +23,7 @@ import org.hibernate.spatial.SpatialFunction;
public class PostgisPG92Dialect extends PostgreSQL92Dialect implements SpatialDialect {
transient private PostgisSupport support = new PostgisSupport();
final transient private PostgisSupport support = new PostgisSupport();
/**
* Creates an instance
@ -45,7 +45,7 @@ public class PostgisPG92Dialect extends PostgreSQL92Dialect implements SpatialDi
typeContributions,
serviceRegistry
);
support.contributeTypes( typeContributions, serviceRegistry );
support.contributeTypes( typeContributions, serviceRegistry, PGGeometryTypeDescriptor.INSTANCE_WKB_1 );
}
@Override

View File

@ -23,7 +23,7 @@ import org.hibernate.spatial.SpatialFunction;
public class PostgisPG93Dialect extends PostgreSQL93Dialect implements SpatialDialect {
transient private PostgisSupport support = new PostgisSupport();
final transient private PostgisSupport support = new PostgisSupport();
/**
* Creates an instance
@ -45,7 +45,7 @@ public class PostgisPG93Dialect extends PostgreSQL93Dialect implements SpatialDi
typeContributions,
serviceRegistry
);
support.contributeTypes( typeContributions, serviceRegistry );
support.contributeTypes( typeContributions, serviceRegistry, PGGeometryTypeDescriptor.INSTANCE_WKB_1 );
}
@Override

View File

@ -23,7 +23,7 @@ import org.hibernate.spatial.SpatialFunction;
public class PostgisPG94Dialect extends PostgreSQL94Dialect implements SpatialDialect {
transient private PostgisSupport support = new PostgisSupport();
final transient private PostgisSupport support = new PostgisSupport();
/**
* Creates an instance
@ -45,7 +45,7 @@ public class PostgisPG94Dialect extends PostgreSQL94Dialect implements SpatialDi
typeContributions,
serviceRegistry
);
support.contributeTypes( typeContributions, serviceRegistry );
support.contributeTypes( typeContributions, serviceRegistry, PGGeometryTypeDescriptor.INSTANCE_WKB_1 );
}
@Override

View File

@ -25,7 +25,7 @@ public class PostgisPG95Dialect extends PostgreSQL95Dialect implements PGSpatial
public PostgisPG95Dialect() {
super();
registerColumnType(
PGGeometryTypeDescriptor.INSTANCE_WKB_1.getSqlType(),
PGGeometryTypeDescriptor.INSTANCE_WKB_2.getSqlType(),
"GEOMETRY"
);
for ( Map.Entry<String, SQLFunction> entry : functionsToRegister() ) {

View File

@ -23,7 +23,7 @@ import org.hibernate.spatial.SpatialFunction;
public class PostgisPG9Dialect extends PostgreSQL9Dialect implements SpatialDialect {
transient private PostgisSupport support = new PostgisSupport();
final transient private PostgisSupport support = new PostgisSupport();
/**
* Creates an instance
@ -31,7 +31,7 @@ public class PostgisPG9Dialect extends PostgreSQL9Dialect implements SpatialDial
public PostgisPG9Dialect() {
super();
registerColumnType(
PGGeometryTypeDescriptor.INSTANCE_WKB_1.getSqlType(),
PGGeometryTypeDescriptor.INSTANCE_WKB_2.getSqlType(),
"GEOMETRY"
);
for ( Map.Entry<String, SQLFunction> entry : support.functionsToRegister() ) {

View File

@ -20,6 +20,7 @@ import org.hibernate.spatial.SpatialDialect;
import org.hibernate.spatial.SpatialFunction;
import org.hibernate.spatial.SpatialRelation;
import org.hibernate.spatial.dialect.SpatialFunctionsRegistry;
import org.hibernate.type.descriptor.sql.SqlTypeDescriptor;
/**
* Created by Karel Maesen, Geovise BVBA on 29/10/16.
@ -36,20 +37,28 @@ public class PostgisSupport implements SpatialDialect, Serializable {
postgisFunctions = new PostgisFunctions();
}
public void contributeTypes(TypeContributions typeContributions, ServiceRegistry serviceRegistry) {
typeContributions.contributeType( new GeolatteGeometryType( PGGeometryTypeDescriptor.INSTANCE_WKB_1 ) );
typeContributions.contributeType( new JTSGeometryType( PGGeometryTypeDescriptor.INSTANCE_WKB_1 ) );
public void contributeTypes(
TypeContributions typeContributions,
ServiceRegistry serviceRegistry,
SqlTypeDescriptor wkbType) {
typeContributions.contributeType( new GeolatteGeometryType( wkbType ) );
typeContributions.contributeType( new JTSGeometryType( wkbType ) );
typeContributions.contributeJavaTypeDescriptor( GeolatteGeometryJavaTypeDescriptor.INSTANCE );
typeContributions.contributeJavaTypeDescriptor( JTSGeometryJavaTypeDescriptor.INSTANCE );
}
public void contributeTypes(TypeContributions typeContributions, ServiceRegistry serviceRegistry) {
contributeTypes( typeContributions, serviceRegistry, PGGeometryTypeDescriptor.INSTANCE_WKB_2 );
}
public SpatialFunctionsRegistry functionsToRegister() {
return postgisFunctions;
}
public boolean isSpatial(int typeCode) {
return typeCode == Types.OTHER || typeCode == PGGeometryTypeDescriptor.INSTANCE_WKB_1.getSqlType();
return typeCode == Types.OTHER || typeCode == PGGeometryTypeDescriptor.INSTANCE_WKB_2.getSqlType();
}
/**
@ -117,18 +126,14 @@ public class PostgisSupport implements SpatialDialect, Serializable {
*/
@Override
public String getSpatialAggregateSQL(String columnName, int aggregation) {
switch ( aggregation ) {
case SpatialAggregate.EXTENT:
final StringBuilder stbuf = new StringBuilder();
stbuf.append( "st_extent(" ).append( columnName ).append( ")::geometry" );
return stbuf.toString();
default:
if ( aggregation == SpatialAggregate.EXTENT ) {
return "st_extent(" + columnName + ")::geometry";
}
throw new IllegalArgumentException(
"Aggregation of type "
+ aggregation + " are not supported by this dialect"
);
}
}
/**
* Returns The SQL fragment when parsing a <code>DWithinExpression</code>.

View File

@ -0,0 +1,73 @@
/*
* 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.postgis;
import java.util.List;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
import org.hibernate.testing.RequiresDialect;
import org.hibernate.testing.TestForIssue;
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
import org.junit.Before;
import org.junit.Test;
import org.geolatte.geom.G2D;
import org.geolatte.geom.Point;
import static org.geolatte.geom.builder.DSL.g;
import static org.geolatte.geom.builder.DSL.point;
import static org.geolatte.geom.crs.CoordinateReferenceSystems.WGS84;
import static org.junit.Assert.assertEquals;
@TestForIssue(jiraKey = "HHH-14932")
@RequiresDialect(PostgisPG95Dialect.class)
public class TestWKBPostgis221 extends BaseCoreFunctionalTestCase {
@Override
protected Class<?>[] getAnnotatedClasses() {
return new Class<?>[] { Foo.class };
}
@Before
public void setup() {
inTransaction( session -> session.persist( new Foo(
1,
point( WGS84 )
) ) );
}
@Test
public void test() {
inTransaction( session -> {
List<Foo> list = session
.createQuery( "from Foo", Foo.class )
.getResultList();
assertEquals( point( WGS84 ), list.get( 0 ).point );
} );
}
@Entity(name = "Foo")
@Table(name = "Foo")
public static class Foo {
@Id
long id;
Point<G2D> point;
public Foo() {
}
public Foo(long id, Point<G2D> point) {
this.id = id;
this.point = point;
}
}
}