Skip to content

Commit a94f98b

Browse files
yasin3061pivovarit
authored andcommitted
BAEL-1334 Guide to Hibernate Spatial (eugenp#3139)
* BAEL-1334 Guide to Hibernate Spatial * BAEL-1334 Guide to Hibernate Spatial Moving the files to hibernate5 from libraries * Reverting the pom file
1 parent 2ad2d4d commit a94f98b

5 files changed

Lines changed: 179 additions & 5 deletions

File tree

‎hibernate5/pom.xml‎

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,15 @@
1717
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
1818
<!-- Maven plugins -->
1919
<maven-compiler-plugin.version>3.6.0</maven-compiler-plugin.version>
20-
</properties>
20+
<hibernate.version>5.2.12.Final</hibernate.version>
21+
<mysql.version>6.0.6</mysql.version>
22+
<mariaDB4j.version>2.2.3</mariaDB4j.version>
23+
</properties>
2124
<dependencies>
2225
<dependency>
2326
<groupId>org.hibernate</groupId>
2427
<artifactId>hibernate-core</artifactId>
25-
<version>5.2.12.Final</version>
28+
<version>${hibernate.version}</version>
2629
</dependency>
2730
<dependency>
2831
<groupId>junit</groupId>
@@ -40,6 +43,21 @@
4043
<artifactId>h2</artifactId>
4144
<version>1.4.194</version>
4245
</dependency>
46+
<dependency>
47+
<groupId>org.hibernate</groupId>
48+
<artifactId>hibernate-spatial</artifactId>
49+
<version>${hibernate.version}</version>
50+
</dependency>
51+
<dependency>
52+
<groupId>mysql</groupId>
53+
<artifactId>mysql-connector-java</artifactId>
54+
<version>${mysql.version}</version>
55+
</dependency>
56+
<dependency>
57+
<groupId>ch.vorburger.mariaDB4j</groupId>
58+
<artifactId>mariaDB4j</artifactId>
59+
<version>${mariaDB4j.version}</version>
60+
</dependency>
4361
</dependencies>
4462
<build>
4563
<finalName>hibernate5</finalName>

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

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import com.baeldung.hibernate.pojo.OrderEntry;
66
import com.baeldung.hibernate.pojo.OrderEntryIdClass;
77
import com.baeldung.hibernate.pojo.OrderEntryPK;
8+
import com.baeldung.hibernate.pojo.PointEntity;
89
import com.baeldung.hibernate.pojo.Product;
910
import com.baeldung.hibernate.pojo.Phone;
1011
import com.baeldung.hibernate.pojo.TemporalValues;
@@ -23,6 +24,7 @@
2324
import com.baeldung.hibernate.pojo.inheritance.Pet;
2425
import com.baeldung.hibernate.pojo.inheritance.Vehicle;
2526

27+
import org.apache.commons.lang3.StringUtils;
2628
import org.hibernate.SessionFactory;
2729
import org.hibernate.boot.Metadata;
2830
import org.hibernate.boot.MetadataSources;
@@ -36,8 +38,14 @@
3638

3739
public class HibernateUtil {
3840
private static SessionFactory sessionFactory;
41+
private static String PROPERTY_FILE_NAME;
3942

4043
public static SessionFactory getSessionFactory() throws IOException {
44+
return getSessionFactory(null);
45+
}
46+
47+
public static SessionFactory getSessionFactory(String propertyFileName) throws IOException {
48+
PROPERTY_FILE_NAME = propertyFileName;
4149
if (sessionFactory == null) {
4250
ServiceRegistry serviceRegistry = configureServiceRegistry();
4351
sessionFactory = makeSessionFactory(serviceRegistry);
@@ -70,6 +78,7 @@ private static SessionFactory makeSessionFactory(ServiceRegistry serviceRegistry
7078
metadataSources.addAnnotatedClass(Vehicle.class);
7179
metadataSources.addAnnotatedClass(Car.class);
7280
metadataSources.addAnnotatedClass(Bag.class);
81+
metadataSources.addAnnotatedClass(PointEntity.class);
7382

7483
Metadata metadata = metadataSources.buildMetadata();
7584
return metadata.getSessionFactoryBuilder()
@@ -86,12 +95,11 @@ private static ServiceRegistry configureServiceRegistry() throws IOException {
8695
private static Properties getProperties() throws IOException {
8796
Properties properties = new Properties();
8897
URL propertiesURL = Thread.currentThread()
89-
.getContextClassLoader()
90-
.getResource("hibernate.properties");
98+
.getContextClassLoader()
99+
.getResource(StringUtils.defaultString(PROPERTY_FILE_NAME, "hibernate.properties"));
91100
try (FileInputStream inputStream = new FileInputStream(propertiesURL.getFile())) {
92101
properties.load(inputStream);
93102
}
94103
return properties;
95104
}
96-
97105
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.baeldung.hibernate.pojo;
2+
3+
import com.vividsolutions.jts.geom.Point;
4+
5+
import javax.persistence.Entity;
6+
import javax.persistence.GeneratedValue;
7+
import javax.persistence.Id;
8+
9+
@Entity
10+
public class PointEntity {
11+
12+
@Id
13+
@GeneratedValue
14+
private Long id;
15+
16+
private Point point;
17+
18+
public PointEntity() {
19+
}
20+
21+
public Long getId() {
22+
return id;
23+
}
24+
25+
public void setId(Long id) {
26+
this.id = id;
27+
}
28+
29+
public Point getPoint() {
30+
return point;
31+
}
32+
33+
public void setPoint(Point point) {
34+
this.point = point;
35+
}
36+
37+
@Override
38+
public String toString() {
39+
return "PointEntity{" + "id=" + id + ", point=" + point + '}';
40+
}
41+
}
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
package com.baeldung.hibernate;
2+
3+
import com.baeldung.hibernate.pojo.PointEntity;
4+
import com.vividsolutions.jts.geom.Geometry;
5+
import com.vividsolutions.jts.geom.Point;
6+
import com.vividsolutions.jts.io.ParseException;
7+
import com.vividsolutions.jts.io.WKTReader;
8+
import org.hibernate.Session;
9+
import org.hibernate.Transaction;
10+
import org.junit.After;
11+
import org.junit.Before;
12+
import org.junit.Test;
13+
14+
import javax.persistence.Query;
15+
import java.io.IOException;
16+
17+
import static org.junit.Assert.assertEquals;
18+
import static org.junit.Assert.assertTrue;
19+
20+
public class HibernateSpatialTest {
21+
22+
private Session session;
23+
private Transaction transaction;
24+
25+
@Before
26+
public void setUp() throws IOException {
27+
session = HibernateUtil.getSessionFactory("hibernate-spatial.properties")
28+
.openSession();
29+
transaction = session.beginTransaction();
30+
}
31+
32+
@After
33+
public void tearDown() {
34+
transaction.rollback();
35+
session.close();
36+
}
37+
38+
@Test
39+
public void shouldConvertWktToGeometry() throws ParseException {
40+
Geometry geometry = wktToGeometry("POINT (2 5)");
41+
assertEquals("Point", geometry.getGeometryType());
42+
assertTrue(geometry instanceof Point);
43+
}
44+
45+
@Test
46+
public void shouldInsertAndSelectPoints() throws ParseException {
47+
PointEntity entity = new PointEntity();
48+
entity.setPoint((Point) wktToGeometry("POINT (1 1)"));
49+
50+
session.persist(entity);
51+
PointEntity fromDb = session.find(PointEntity.class, entity.getId());
52+
assertEquals("POINT (1 1)", fromDb.getPoint().toString());
53+
}
54+
55+
@Test
56+
public void shouldSelectDisjointPoints() throws ParseException {
57+
insertPoint("POINT (1 2)");
58+
insertPoint("POINT (3 4)");
59+
insertPoint("POINT (5 6)");
60+
61+
Point point = (Point) wktToGeometry("POINT (3 4)");
62+
Query query = session.createQuery("select p from PointEntity p "
63+
+ "where disjoint(p.point, :point) = true", PointEntity.class);
64+
query.setParameter("point", point);
65+
assertEquals("POINT (1 2)", ((PointEntity) query.getResultList().get(0)).getPoint().toString());
66+
assertEquals("POINT (5 6)", ((PointEntity) query.getResultList().get(1)).getPoint().toString());
67+
}
68+
69+
@Test
70+
public void shouldSelectAllPointsWithinPolygon() throws ParseException {
71+
insertPoint("POINT (1 1)");
72+
insertPoint("POINT (1 2)");
73+
insertPoint("POINT (3 4)");
74+
insertPoint("POINT (5 6)");
75+
76+
Query query = session.createQuery("select p from PointEntity p where within(p.point, :area) = true",
77+
PointEntity.class);
78+
query.setParameter("area", wktToGeometry("POLYGON((0 0, 0 5, 5 5, 5 0, 0 0))"));
79+
assertEquals(3, query.getResultList().size());
80+
assertEquals("POINT (1 1)", ((PointEntity) query.getResultList().get(0)).getPoint().toString());
81+
assertEquals("POINT (1 2)", ((PointEntity) query.getResultList().get(1)).getPoint().toString());
82+
assertEquals("POINT (3 4)", ((PointEntity) query.getResultList().get(2)).getPoint().toString());
83+
}
84+
85+
private void insertPoint(String point) throws ParseException {
86+
PointEntity entity = new PointEntity();
87+
entity.setPoint((Point) wktToGeometry(point));
88+
session.persist(entity);
89+
}
90+
91+
private Geometry wktToGeometry(String wellKnownText) throws ParseException {
92+
WKTReader fromText = new WKTReader();
93+
Geometry geom = null;
94+
geom = fromText.read(wellKnownText);
95+
return geom;
96+
}
97+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
hibernate.dialect=org.hibernate.spatial.dialect.mysql.MySQL56SpatialDialect
2+
hibernate.connection.driver_class=com.mysql.jdbc.Driver
3+
hibernate.connection.url=jdbc:mysql://localhost:3306/hibernate-spatial
4+
hibernate.connection.username=root
5+
hibernate.connection.password=pass
6+
hibernate.connection.pool_size=5
7+
hibernate.show_sql=true
8+
hibernate.format_sql=true
9+
hibernate.max_fetch_depth=5
10+
hibernate.hbm2ddl.auto=create-drop

0 commit comments

Comments
 (0)