Skip to content

Commit 216e61f

Browse files
vladmihalceamaesenka
authored andcommitted
HHH-11764 - JTS geometry being bound to byte array instead of PGgeometry
1 parent 3f2c36a commit 216e61f

File tree

4 files changed

+145
-6
lines changed

4 files changed

+145
-6
lines changed

databases.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ ext {
5252
'jdbc.url' : 'jdbc:mariadb://127.0.0.1/hibernate_orm_test'
5353
],
5454
postgis : [
55-
'db.dialect' : 'org.hibernate.spatial.dialect.postgis.PostgisDialect',
55+
'db.dialect' : 'org.hibernate.spatial.dialect.postgis.PostgisPG95Dialect',
5656
'jdbc.driver': 'org.postgresql.Driver',
5757
'jdbc.user' : 'hibernate_orm_test',
5858
'jdbc.pass' : 'hibernate_orm_test',

documentation/src/test/java/org/hibernate/userguide/spatial/SpatialTest.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
import org.hibernate.jpa.test.BaseEntityManagerFunctionalTestCase;
1313
import org.hibernate.spatial.dialect.postgis.PostgisDialect;
14+
import org.hibernate.spatial.dialect.postgis.PostgisPG95Dialect;
1415

1516
import org.hibernate.testing.RequiresDialect;
1617
import org.junit.Test;
@@ -30,10 +31,10 @@
3031
/**
3132
* @author Vlad Mihalcea
3233
*/
33-
@RequiresDialect(PostgisDialect.class)
34+
@RequiresDialect(PostgisPG95Dialect.class)
3435
public class SpatialTest extends BaseEntityManagerFunctionalTestCase {
3536

36-
GeometryFactory geometryFactory = new GeometryFactory();
37+
private GeometryFactory geometryFactory = new GeometryFactory();
3738

3839
@Override
3940
protected Class<?>[] getAnnotatedClasses() {

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

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -139,9 +139,7 @@ class PostgisFunctions extends SpatialFunctionsRegistry {
139139
)
140140
);
141141
put(
142-
"buffer", new StandardSQLFunction(
143-
"st_buffer"
144-
)
142+
"buffer", new BufferFunction()
145143
);
146144
put(
147145
"convexhull", new StandardSQLFunction(
@@ -201,4 +199,29 @@ public String render(
201199
return rendered + "::geometry";
202200
}
203201
}
202+
203+
private static class BufferFunction extends StandardSQLFunction {
204+
205+
public BufferFunction() {
206+
super( "st_buffer" );
207+
}
208+
209+
@Override
210+
public String render(
211+
Type firstArgumentType, List arguments, SessionFactoryImplementor sessionFactory) {
212+
final StringBuilder buf = new StringBuilder();
213+
buf.append( getName() ).append( '(' );
214+
for ( int i = 0; i < arguments.size(); i++ ) {
215+
buf.append( arguments.get( i ) );
216+
if( i == 0 ) {
217+
buf.append( "::geometry" );
218+
}
219+
if ( i < arguments.size() - 1 ) {
220+
buf.append( ", " );
221+
}
222+
}
223+
return buf.append( ")::geometry" ).toString();
224+
}
225+
}
226+
204227
}
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
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+
package org.hibernate.spatial.testing.dialects.postgis;
8+
9+
import java.util.List;
10+
import javax.persistence.Entity;
11+
import javax.persistence.Id;
12+
13+
import org.hibernate.spatial.dialect.postgis.PostgisPG95Dialect;
14+
15+
import org.hibernate.testing.RequiresDialect;
16+
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
17+
import org.junit.Test;
18+
19+
import com.vividsolutions.jts.geom.Coordinate;
20+
import com.vividsolutions.jts.geom.Geometry;
21+
import com.vividsolutions.jts.geom.GeometryFactory;
22+
import com.vividsolutions.jts.geom.Point;
23+
import com.vividsolutions.jts.geom.Polygon;
24+
25+
import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate;
26+
import static org.junit.Assert.assertEquals;
27+
28+
/**
29+
* @author Vlad Mihalcea
30+
*/
31+
@RequiresDialect(PostgisPG95Dialect.class)
32+
public class PostgisBufferTest extends BaseCoreFunctionalTestCase {
33+
34+
private GeometryFactory geometryFactory = new GeometryFactory();
35+
36+
@Override
37+
protected Class<?>[] getAnnotatedClasses() {
38+
return new Class<?>[] {
39+
Event.class,
40+
};
41+
}
42+
43+
@Test
44+
public void test() {
45+
Long addressId = doInHibernate( this::sessionFactory, session -> {
46+
Event event = new Event();
47+
event.setId( 1L);
48+
event.setName( "Hibernate ORM presentation");
49+
Point point = geometryFactory.createPoint( new Coordinate( 10, 5 ) );
50+
event.setLocation( point );
51+
52+
session.persist( event );
53+
return event.getId();
54+
});
55+
56+
doInHibernate( this::sessionFactory, session -> {
57+
Coordinate [] coordinates = new Coordinate[] {
58+
new Coordinate(1,1), new Coordinate(20,1), new Coordinate(20,20),
59+
new Coordinate(1,20), new Coordinate(1,1)
60+
};
61+
Polygon window = geometryFactory.createPolygon( coordinates );
62+
63+
List<Event> events = session.createQuery(
64+
"select e " +
65+
"from Event e " +
66+
"where buffer(:window, 100) is not null", Event.class)
67+
.setParameter("window", window)
68+
.getResultList();
69+
70+
assertEquals(1, events.size());
71+
72+
List<Geometry> locations = session.createQuery(
73+
"select buffer(e.location, 10) " +
74+
"from Event e ", Geometry.class)
75+
.getResultList();
76+
77+
assertEquals(1, locations.size());
78+
});
79+
}
80+
81+
@Entity(name = "Event")
82+
public static class Event {
83+
84+
@Id
85+
private Long id;
86+
87+
private String name;
88+
89+
private Point location;
90+
91+
public Long getId() {
92+
return id;
93+
}
94+
95+
public void setId(Long id) {
96+
this.id = id;
97+
}
98+
99+
public String getName() {
100+
return name;
101+
}
102+
103+
public void setName(String name) {
104+
this.name = name;
105+
}
106+
107+
public Point getLocation() {
108+
return location;
109+
}
110+
111+
public void setLocation(Point location) {
112+
this.location = location;
113+
}
114+
}
115+
}

0 commit comments

Comments
 (0)