Skip to content

Commit 29b896b

Browse files
committed
HHH-14932 Use the correct WKB Dialect
1 parent 6fae23a commit 29b896b

File tree

10 files changed

+114
-35
lines changed

10 files changed

+114
-35
lines changed

hibernate-spatial/src/main/java/org/hibernate/spatial/dialect/postgis/PGGeometryTypeDescriptor.java

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,11 @@ public Geometry<?> toGeometry(Object object) {
5353
if ( object == null ) {
5454
return null;
5555
}
56-
ByteBuffer buffer = null;
56+
ByteBuffer buffer;
5757
if ( object instanceof PGobject ) {
5858
String pgValue = ( (PGobject) object ).getValue();
5959

60+
assert pgValue != null;
6061
if ( pgValue.startsWith( "00" ) || pgValue.startsWith( "01" ) ) {
6162
//we have a WKB because this pgValue starts with the bit-order byte
6263
buffer = ByteBuffer.from( pgValue );
@@ -91,7 +92,7 @@ public <X> ValueBinder<X> getBinder(final JavaTypeDescriptor<X> javaTypeDescript
9192
return new ValueBinder<X>() {
9293

9394
@Override
94-
public final void bind(PreparedStatement st, X value, int index, WrapperOptions options)
95+
public void bind(PreparedStatement st, X value, int index, WrapperOptions options)
9596
throws SQLException {
9697
if ( value == null ) {
9798
st.setNull( index, Types.OTHER );
@@ -102,7 +103,7 @@ public final void bind(PreparedStatement st, X value, int index, WrapperOptions
102103
}
103104

104105
@Override
105-
public final void bind(CallableStatement st, X value, String name, WrapperOptions options)
106+
public void bind(CallableStatement st, X value, String name, WrapperOptions options)
106107
throws SQLException {
107108
if ( value == null ) {
108109
st.setNull( name, Types.OTHER );
@@ -112,21 +113,21 @@ public final void bind(CallableStatement st, X value, String name, WrapperOption
112113
}
113114
}
114115

115-
protected void doBind(PreparedStatement st, X value, int index, WrapperOptions options)
116+
private void doBind(PreparedStatement st, X value, int index, WrapperOptions options)
116117
throws SQLException {
117118
final PGobject obj = toPGobject( value, options );
118119
st.setObject( index, obj );
119120
}
120121

121-
protected void doBind(CallableStatement st, X value, String name, WrapperOptions options)
122+
private void doBind(CallableStatement st, X value, String name, WrapperOptions options)
122123
throws SQLException {
123124
final PGobject obj = toPGobject( value, options );
124125
st.setObject( name, obj );
125126
}
126127

127128
private PGobject toPGobject(X value, WrapperOptions options) throws SQLException {
128-
final WkbEncoder encoder = Wkb.newEncoder( Wkb.Dialect.POSTGIS_EWKB_1 );
129-
final Geometry geometry = javaTypeDescriptor.unwrap( value, Geometry.class, options );
129+
final WkbEncoder encoder = Wkb.newEncoder( wkbDialect );
130+
final Geometry<?> geometry = javaTypeDescriptor.unwrap( value, Geometry.class, options );
130131
final String hexString = encoder.encode( geometry, ByteOrder.NDR ).toString();
131132
final PGobject obj = new PGobject();
132133
obj.setType( "geometry" );

hibernate-spatial/src/main/java/org/hibernate/spatial/dialect/postgis/PostgisPG82Dialect.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
public class PostgisPG82Dialect extends PostgreSQL82Dialect implements SpatialDialect {
2424

2525

26-
transient private PostgisSupport support = new PostgisSupport();
26+
final transient private PostgisSupport support = new PostgisSupport();
2727

2828
/**
2929
* Creates an instance
@@ -45,7 +45,7 @@ public void contributeTypes(TypeContributions typeContributions, ServiceRegistry
4545
typeContributions,
4646
serviceRegistry
4747
);
48-
support.contributeTypes( typeContributions, serviceRegistry );
48+
support.contributeTypes( typeContributions, serviceRegistry, PGGeometryTypeDescriptor.INSTANCE_WKB_1 );
4949
}
5050

5151
@Override

hibernate-spatial/src/main/java/org/hibernate/spatial/dialect/postgis/PostgisPG91Dialect.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
public class PostgisPG91Dialect extends PostgreSQL91Dialect implements SpatialDialect {
2424

2525

26-
transient private PostgisSupport support = new PostgisSupport();
26+
final transient private PostgisSupport support = new PostgisSupport();
2727

2828
/**
2929
* Creates an instance
@@ -45,7 +45,7 @@ public void contributeTypes(TypeContributions typeContributions, ServiceRegistry
4545
typeContributions,
4646
serviceRegistry
4747
);
48-
support.contributeTypes( typeContributions, serviceRegistry );
48+
support.contributeTypes( typeContributions, serviceRegistry, PGGeometryTypeDescriptor.INSTANCE_WKB_1 );
4949
}
5050

5151
@Override

hibernate-spatial/src/main/java/org/hibernate/spatial/dialect/postgis/PostgisPG92Dialect.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
public class PostgisPG92Dialect extends PostgreSQL92Dialect implements SpatialDialect {
2424

2525

26-
transient private PostgisSupport support = new PostgisSupport();
26+
final transient private PostgisSupport support = new PostgisSupport();
2727

2828
/**
2929
* Creates an instance
@@ -45,7 +45,7 @@ public void contributeTypes(TypeContributions typeContributions, ServiceRegistry
4545
typeContributions,
4646
serviceRegistry
4747
);
48-
support.contributeTypes( typeContributions, serviceRegistry );
48+
support.contributeTypes( typeContributions, serviceRegistry, PGGeometryTypeDescriptor.INSTANCE_WKB_1 );
4949
}
5050

5151
@Override

hibernate-spatial/src/main/java/org/hibernate/spatial/dialect/postgis/PostgisPG93Dialect.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
public class PostgisPG93Dialect extends PostgreSQL93Dialect implements SpatialDialect {
2424

2525

26-
transient private PostgisSupport support = new PostgisSupport();
26+
final transient private PostgisSupport support = new PostgisSupport();
2727

2828
/**
2929
* Creates an instance
@@ -45,7 +45,7 @@ public void contributeTypes(TypeContributions typeContributions, ServiceRegistry
4545
typeContributions,
4646
serviceRegistry
4747
);
48-
support.contributeTypes( typeContributions, serviceRegistry );
48+
support.contributeTypes( typeContributions, serviceRegistry, PGGeometryTypeDescriptor.INSTANCE_WKB_1 );
4949
}
5050

5151
@Override

hibernate-spatial/src/main/java/org/hibernate/spatial/dialect/postgis/PostgisPG94Dialect.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
public class PostgisPG94Dialect extends PostgreSQL94Dialect implements SpatialDialect {
2424

2525

26-
transient private PostgisSupport support = new PostgisSupport();
26+
final transient private PostgisSupport support = new PostgisSupport();
2727

2828
/**
2929
* Creates an instance
@@ -45,7 +45,7 @@ public void contributeTypes(TypeContributions typeContributions, ServiceRegistry
4545
typeContributions,
4646
serviceRegistry
4747
);
48-
support.contributeTypes( typeContributions, serviceRegistry );
48+
support.contributeTypes( typeContributions, serviceRegistry, PGGeometryTypeDescriptor.INSTANCE_WKB_1 );
4949
}
5050

5151
@Override

hibernate-spatial/src/main/java/org/hibernate/spatial/dialect/postgis/PostgisPG95Dialect.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public class PostgisPG95Dialect extends PostgreSQL95Dialect implements PGSpatial
2525
public PostgisPG95Dialect() {
2626
super();
2727
registerColumnType(
28-
PGGeometryTypeDescriptor.INSTANCE_WKB_1.getSqlType(),
28+
PGGeometryTypeDescriptor.INSTANCE_WKB_2.getSqlType(),
2929
"GEOMETRY"
3030
);
3131
for ( Map.Entry<String, SQLFunction> entry : functionsToRegister() ) {

hibernate-spatial/src/main/java/org/hibernate/spatial/dialect/postgis/PostgisPG9Dialect.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,15 @@
2323
public class PostgisPG9Dialect extends PostgreSQL9Dialect implements SpatialDialect {
2424

2525

26-
transient private PostgisSupport support = new PostgisSupport();
26+
final transient private PostgisSupport support = new PostgisSupport();
2727

2828
/**
2929
* Creates an instance
3030
*/
3131
public PostgisPG9Dialect() {
3232
super();
3333
registerColumnType(
34-
PGGeometryTypeDescriptor.INSTANCE_WKB_1.getSqlType(),
34+
PGGeometryTypeDescriptor.INSTANCE_WKB_2.getSqlType(),
3535
"GEOMETRY"
3636
);
3737
for ( Map.Entry<String, SQLFunction> entry : support.functionsToRegister() ) {

hibernate-spatial/src/main/java/org/hibernate/spatial/dialect/postgis/PostgisSupport.java

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import org.hibernate.spatial.SpatialFunction;
2121
import org.hibernate.spatial.SpatialRelation;
2222
import org.hibernate.spatial.dialect.SpatialFunctionsRegistry;
23+
import org.hibernate.type.descriptor.sql.SqlTypeDescriptor;
2324

2425
/**
2526
* Created by Karel Maesen, Geovise BVBA on 29/10/16.
@@ -36,20 +37,28 @@ public PostgisSupport() {
3637
postgisFunctions = new PostgisFunctions();
3738
}
3839

39-
public void contributeTypes(TypeContributions typeContributions, ServiceRegistry serviceRegistry) {
40-
typeContributions.contributeType( new GeolatteGeometryType( PGGeometryTypeDescriptor.INSTANCE_WKB_1 ) );
41-
typeContributions.contributeType( new JTSGeometryType( PGGeometryTypeDescriptor.INSTANCE_WKB_1 ) );
40+
public void contributeTypes(
41+
TypeContributions typeContributions,
42+
ServiceRegistry serviceRegistry,
43+
SqlTypeDescriptor wkbType) {
44+
typeContributions.contributeType( new GeolatteGeometryType( wkbType ) );
45+
typeContributions.contributeType( new JTSGeometryType( wkbType ) );
4246

4347
typeContributions.contributeJavaTypeDescriptor( GeolatteGeometryJavaTypeDescriptor.INSTANCE );
4448
typeContributions.contributeJavaTypeDescriptor( JTSGeometryJavaTypeDescriptor.INSTANCE );
4549
}
4650

51+
public void contributeTypes(TypeContributions typeContributions, ServiceRegistry serviceRegistry) {
52+
contributeTypes( typeContributions, serviceRegistry, PGGeometryTypeDescriptor.INSTANCE_WKB_2 );
53+
}
54+
55+
4756
public SpatialFunctionsRegistry functionsToRegister() {
4857
return postgisFunctions;
4958
}
5059

51-
public boolean isSpatial(int typeCode){
52-
return typeCode == Types.OTHER || typeCode == PGGeometryTypeDescriptor.INSTANCE_WKB_1.getSqlType();
60+
public boolean isSpatial(int typeCode) {
61+
return typeCode == Types.OTHER || typeCode == PGGeometryTypeDescriptor.INSTANCE_WKB_2.getSqlType();
5362
}
5463

5564
/**
@@ -117,17 +126,13 @@ public String getSpatialFilterExpression(String columnName) {
117126
*/
118127
@Override
119128
public String getSpatialAggregateSQL(String columnName, int aggregation) {
120-
switch ( aggregation ) {
121-
case SpatialAggregate.EXTENT:
122-
final StringBuilder stbuf = new StringBuilder();
123-
stbuf.append( "st_extent(" ).append( columnName ).append( ")::geometry" );
124-
return stbuf.toString();
125-
default:
126-
throw new IllegalArgumentException(
127-
"Aggregation of type "
128-
+ aggregation + " are not supported by this dialect"
129-
);
129+
if ( aggregation == SpatialAggregate.EXTENT ) {
130+
return "st_extent(" + columnName + ")::geometry";
130131
}
132+
throw new IllegalArgumentException(
133+
"Aggregation of type "
134+
+ aggregation + " are not supported by this dialect"
135+
);
131136
}
132137

133138
/**
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/*
2+
* Hibernate, Relational Persistence for Idiomatic Java
3+
*
4+
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
5+
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
6+
*/
7+
8+
package org.hibernate.spatial.dialect.postgis;
9+
10+
import java.util.List;
11+
import javax.persistence.Entity;
12+
import javax.persistence.Id;
13+
import javax.persistence.Table;
14+
15+
import org.hibernate.testing.RequiresDialect;
16+
import org.hibernate.testing.TestForIssue;
17+
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
18+
import org.junit.Before;
19+
import org.junit.Test;
20+
21+
import org.geolatte.geom.G2D;
22+
import org.geolatte.geom.Point;
23+
24+
import static org.geolatte.geom.builder.DSL.g;
25+
import static org.geolatte.geom.builder.DSL.point;
26+
import static org.geolatte.geom.crs.CoordinateReferenceSystems.WGS84;
27+
import static org.junit.Assert.assertEquals;
28+
29+
30+
@TestForIssue(jiraKey = "HHH-14932")
31+
@RequiresDialect(PostgisPG95Dialect.class)
32+
public class TestWKBPostgis221 extends BaseCoreFunctionalTestCase {
33+
34+
@Override
35+
protected Class<?>[] getAnnotatedClasses() {
36+
return new Class<?>[] { Foo.class };
37+
}
38+
39+
@Before
40+
public void setup() {
41+
inTransaction( session -> session.persist( new Foo(
42+
1,
43+
point( WGS84 )
44+
) ) );
45+
}
46+
47+
@Test
48+
public void test() {
49+
inTransaction( session -> {
50+
List<Foo> list = session
51+
.createQuery( "from Foo", Foo.class )
52+
.getResultList();
53+
assertEquals( point( WGS84 ), list.get( 0 ).point );
54+
} );
55+
}
56+
57+
@Entity(name = "Foo")
58+
@Table(name = "Foo")
59+
public static class Foo {
60+
@Id
61+
long id;
62+
Point<G2D> point;
63+
64+
public Foo() {
65+
}
66+
67+
public Foo(long id, Point<G2D> point) {
68+
this.id = id;
69+
this.point = point;
70+
}
71+
72+
}
73+
}

0 commit comments

Comments
 (0)