Skip to content

Commit f9f5a82

Browse files
YasinBhojawalapivovarit
authored andcommitted
BAEL-1334 Guide to Hibernate Spatial (eugenp#3211)
* BAEL-1334 Guide to Hibernate Spatial * BAEL-1334 Guide to Hibernate Spatial Moving the files to hibernate5 from libraries * Reverting the pom file * BAEL-1334 Guide to Hibernate Spatial * BAEL-1334 Guide to Hibernate Spatial Moving the files to hibernate5 from libraries * Reverting the pom file * BAEL-1334 Guide to Hibernate Spatial Improved assertions * Add examples related to circle
1 parent 763f0d5 commit f9f5a82

3 files changed

Lines changed: 87 additions & 0 deletions

File tree

‎hibernate5/src/main/java/com/baeldung/hibernate/HibernateUtil.java‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import com.baeldung.hibernate.pojo.OrderEntryIdClass;
77
import com.baeldung.hibernate.pojo.OrderEntryPK;
88
import com.baeldung.hibernate.pojo.PointEntity;
9+
import com.baeldung.hibernate.pojo.PolygonEntity;
910
import com.baeldung.hibernate.pojo.Product;
1011
import com.baeldung.hibernate.pojo.Phone;
1112
import com.baeldung.hibernate.pojo.TemporalValues;
@@ -79,6 +80,7 @@ private static SessionFactory makeSessionFactory(ServiceRegistry serviceRegistry
7980
metadataSources.addAnnotatedClass(Car.class);
8081
metadataSources.addAnnotatedClass(Bag.class);
8182
metadataSources.addAnnotatedClass(PointEntity.class);
83+
metadataSources.addAnnotatedClass(PolygonEntity.class);
8284

8385
Metadata metadata = metadataSources.buildMetadata();
8486
return metadata.getSessionFactoryBuilder()
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.baeldung.hibernate.pojo;
2+
3+
import com.vividsolutions.jts.geom.Polygon;
4+
5+
import javax.persistence.Entity;
6+
import javax.persistence.GeneratedValue;
7+
import javax.persistence.Id;
8+
9+
@Entity
10+
public class PolygonEntity {
11+
12+
@Id
13+
@GeneratedValue
14+
private Long id;
15+
16+
private Polygon polygon;
17+
18+
public Long getId() {
19+
return id;
20+
}
21+
22+
public void setId(Long id) {
23+
this.id = id;
24+
}
25+
26+
public Polygon getPolygon() {
27+
return polygon;
28+
}
29+
30+
public void setPolygon(Polygon polygon) {
31+
this.polygon = polygon;
32+
}
33+
34+
@Override
35+
public String toString() {
36+
return "PolygonEntity{" + "id=" + id + ", polygon=" + polygon + '}';
37+
}
38+
}

‎hibernate5/src/test/java/com/baeldung/hibernate/HibernateSpatialTest.java‎

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
package com.baeldung.hibernate;
22

33
import com.baeldung.hibernate.pojo.PointEntity;
4+
import com.baeldung.hibernate.pojo.PolygonEntity;
5+
import com.vividsolutions.jts.geom.Coordinate;
46
import com.vividsolutions.jts.geom.Geometry;
57
import com.vividsolutions.jts.geom.Point;
8+
import com.vividsolutions.jts.geom.Polygon;
69
import com.vividsolutions.jts.io.ParseException;
710
import com.vividsolutions.jts.io.WKTReader;
11+
import com.vividsolutions.jts.util.GeometricShapeFactory;
812
import org.hibernate.Session;
913
import org.hibernate.Transaction;
1014
import org.junit.After;
@@ -13,6 +17,7 @@
1317

1418
import javax.persistence.Query;
1519
import java.io.IOException;
20+
import java.util.List;
1621

1722
import static org.assertj.core.api.Assertions.assertThat;
1823
import static org.junit.Assert.assertEquals;
@@ -82,16 +87,58 @@ public void shouldSelectAllPointsWithinPolygon() throws ParseException {
8287
.containsOnly("POINT (1 1)", "POINT (1 2)", "POINT (3 4)");
8388
}
8489

90+
@Test
91+
public void shouldSelectAllPointsWithinRadius() throws ParseException {
92+
insertPoint("POINT (1 1)");
93+
insertPoint("POINT (1 2)");
94+
insertPoint("POINT (3 4)");
95+
insertPoint("POINT (5 6)");
96+
97+
Query query = session.createQuery("select p from PointEntity p where within(p.point, :circle) = true",
98+
PointEntity.class);
99+
query.setParameter("circle", createCircle(0.0, 0.0, 5));
100+
101+
assertThat(query.getResultList().stream().map(p -> ((PointEntity) p).getPoint().toString()))
102+
.containsOnly("POINT (1 1)", "POINT (1 2)");
103+
}
104+
105+
@Test
106+
public void shouldSelectAdjacentPolygons() throws ParseException {
107+
insertPolygon("POLYGON ((0 0, 0 5, 5 5, 5 0, 0 0))");
108+
insertPolygon("POLYGON ((3 0, 3 5, 8 5, 8 0, 3 0))");
109+
insertPolygon("POLYGON ((2 2, 3 1, 2 5, 4 3, 3 3, 2 2))");
110+
111+
Query query = session.createQuery("select p from PolygonEntity p where touches(p.polygon, :polygon) = true",
112+
PolygonEntity.class);
113+
query.setParameter("polygon", wktToGeometry("POLYGON ((5 5, 5 10, 10 10, 10 5, 5 5))"));
114+
assertThat(query.getResultList().stream().map(p -> ((PolygonEntity) p).getPolygon().toString()))
115+
.containsOnly("POLYGON ((0 0, 0 5, 5 5, 5 0, 0 0))", "POLYGON ((3 0, 3 5, 8 5, 8 0, 3 0))");
116+
}
117+
85118
private void insertPoint(String point) throws ParseException {
86119
PointEntity entity = new PointEntity();
87120
entity.setPoint((Point) wktToGeometry(point));
88121
session.persist(entity);
89122
}
90123

124+
private void insertPolygon(String polygon) throws ParseException {
125+
PolygonEntity entity = new PolygonEntity();
126+
entity.setPolygon((Polygon) wktToGeometry(polygon));
127+
session.persist(entity);
128+
}
129+
91130
private Geometry wktToGeometry(String wellKnownText) throws ParseException {
92131
WKTReader fromText = new WKTReader();
93132
Geometry geom = null;
94133
geom = fromText.read(wellKnownText);
95134
return geom;
96135
}
136+
137+
private static Geometry createCircle(double x, double y, double radius) {
138+
GeometricShapeFactory shapeFactory = new GeometricShapeFactory();
139+
shapeFactory.setNumPoints(32);
140+
shapeFactory.setCentre(new Coordinate(x, y));
141+
shapeFactory.setSize(radius * 2);
142+
return shapeFactory.createCircle();
143+
}
97144
}

0 commit comments

Comments
 (0)