HHH-14932 Using correct WKB version for Postgis

This commit is contained in:
Karel Maesen 2022-02-02 21:59:51 +01:00
parent 285c2099c8
commit c072ee9cb2
3 changed files with 78 additions and 4 deletions

View File

@ -46,8 +46,6 @@ public class PGGeometryJdbcType implements JdbcType {
private final Wkb.Dialect wkbDialect; private final Wkb.Dialect wkbDialect;
// Type descriptor instance using EWKB v1 (postgis versions < 2.2.2)
public static final PGGeometryJdbcType INSTANCE_WKB_1 = new PGGeometryJdbcType( Wkb.Dialect.POSTGIS_EWKB_1 );
// Type descriptor instance using EWKB v2 (postgis versions >= 2.2.2, see: https://trac.osgeo.org/postgis/ticket/3181) // Type descriptor instance using EWKB v2 (postgis versions >= 2.2.2, see: https://trac.osgeo.org/postgis/ticket/3181)
public static final PGGeometryJdbcType INSTANCE_WKB_2 = new PGGeometryJdbcType( Wkb.Dialect.POSTGIS_EWKB_2 ); public static final PGGeometryJdbcType INSTANCE_WKB_2 = new PGGeometryJdbcType( Wkb.Dialect.POSTGIS_EWKB_2 );
@ -118,7 +116,7 @@ public class PGGeometryJdbcType implements JdbcType {
} }
private PGobject toPGobject(X value, WrapperOptions options) throws SQLException { private PGobject toPGobject(X value, WrapperOptions options) throws SQLException {
final WkbEncoder encoder = Wkb.newEncoder( Wkb.Dialect.POSTGIS_EWKB_1 ); final WkbEncoder encoder = Wkb.newEncoder( wkbDialect );
final Geometry<?> geometry = getJavaType().unwrap( value, Geometry.class, options ); final Geometry<?> geometry = getJavaType().unwrap( value, Geometry.class, options );
final String hexString = encoder.encode( geometry, ByteOrder.NDR ).toString(); final String hexString = encoder.encode( geometry, ByteOrder.NDR ).toString();
final PGobject obj = new PGobject(); final PGobject obj = new PGobject();

View File

@ -69,7 +69,7 @@ public class PostgisUnmarshalTest {
public void testCase(String pgValue, Geometry<?> expected) throws SQLException { public void testCase(String pgValue, Geometry<?> expected) throws SQLException {
PGobject pgo = new PGobject(); PGobject pgo = new PGobject();
pgo.setValue( pgValue ); pgo.setValue( pgValue );
Geometry<?> received = PGGeometryJdbcType.INSTANCE_WKB_1.toGeometry( pgo ); Geometry<?> received = PGGeometryJdbcType.INSTANCE_WKB_2.toGeometry( pgo );
assertEquals( String.format( "Failure on %s", pgValue ), expected, received ); assertEquals( String.format( "Failure on %s", pgValue ), expected, received );
} }

View File

@ -0,0 +1,76 @@
/*
* 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.hhh14932;
import java.util.List;
import org.hibernate.dialect.PostgreSQLDialect;
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 jakarta.persistence.Entity;
import jakarta.persistence.Id;
import jakarta.persistence.Table;
import org.geolatte.geom.G2D;
import org.geolatte.geom.Point;
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(PostgreSQLDialect.class)
public class TestUsingPostgisWkb221AndLater 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;
}
}
}