diff --git a/hibernate5/pom.xml b/hibernate5/pom.xml
index 8543d1dae3..3b0b2fcd88 100644
--- a/hibernate5/pom.xml
+++ b/hibernate5/pom.xml
@@ -17,12 +17,15 @@
UTF-8
3.6.0
-
+ 5.2.12.Final
+ 6.0.6
+ 2.2.3
+
org.hibernate
hibernate-core
- 5.2.12.Final
+ ${hibernate.version}
junit
@@ -40,6 +43,21 @@
h2
1.4.194
+
+ org.hibernate
+ hibernate-spatial
+ ${hibernate.version}
+
+
+ mysql
+ mysql-connector-java
+ ${mysql.version}
+
+
+ ch.vorburger.mariaDB4j
+ mariaDB4j
+ ${mariaDB4j.version}
+
hibernate5
diff --git a/hibernate5/src/main/java/com/baeldung/hibernate/HibernateUtil.java b/hibernate5/src/main/java/com/baeldung/hibernate/HibernateUtil.java
index c3bf8c2aa9..f1fc22d29a 100644
--- a/hibernate5/src/main/java/com/baeldung/hibernate/HibernateUtil.java
+++ b/hibernate5/src/main/java/com/baeldung/hibernate/HibernateUtil.java
@@ -5,6 +5,7 @@ import com.baeldung.hibernate.pojo.EntityDescription;
import com.baeldung.hibernate.pojo.OrderEntry;
import com.baeldung.hibernate.pojo.OrderEntryIdClass;
import com.baeldung.hibernate.pojo.OrderEntryPK;
+import com.baeldung.hibernate.pojo.PointEntity;
import com.baeldung.hibernate.pojo.Product;
import com.baeldung.hibernate.pojo.Phone;
import com.baeldung.hibernate.pojo.TemporalValues;
@@ -23,6 +24,7 @@ import com.baeldung.hibernate.pojo.inheritance.Person;
import com.baeldung.hibernate.pojo.inheritance.Pet;
import com.baeldung.hibernate.pojo.inheritance.Vehicle;
+import org.apache.commons.lang3.StringUtils;
import org.hibernate.SessionFactory;
import org.hibernate.boot.Metadata;
import org.hibernate.boot.MetadataSources;
@@ -36,8 +38,14 @@ import java.util.Properties;
public class HibernateUtil {
private static SessionFactory sessionFactory;
+ private static String PROPERTY_FILE_NAME;
public static SessionFactory getSessionFactory() throws IOException {
+ return getSessionFactory(null);
+ }
+
+ public static SessionFactory getSessionFactory(String propertyFileName) throws IOException {
+ PROPERTY_FILE_NAME = propertyFileName;
if (sessionFactory == null) {
ServiceRegistry serviceRegistry = configureServiceRegistry();
sessionFactory = makeSessionFactory(serviceRegistry);
@@ -70,6 +78,7 @@ public class HibernateUtil {
metadataSources.addAnnotatedClass(Vehicle.class);
metadataSources.addAnnotatedClass(Car.class);
metadataSources.addAnnotatedClass(Bag.class);
+ metadataSources.addAnnotatedClass(PointEntity.class);
Metadata metadata = metadataSources.buildMetadata();
return metadata.getSessionFactoryBuilder()
@@ -86,12 +95,11 @@ public class HibernateUtil {
private static Properties getProperties() throws IOException {
Properties properties = new Properties();
URL propertiesURL = Thread.currentThread()
- .getContextClassLoader()
- .getResource("hibernate.properties");
+ .getContextClassLoader()
+ .getResource(StringUtils.defaultString(PROPERTY_FILE_NAME, "hibernate.properties"));
try (FileInputStream inputStream = new FileInputStream(propertiesURL.getFile())) {
properties.load(inputStream);
}
return properties;
}
-
}
\ No newline at end of file
diff --git a/hibernate5/src/main/java/com/baeldung/hibernate/pojo/PointEntity.java b/hibernate5/src/main/java/com/baeldung/hibernate/pojo/PointEntity.java
new file mode 100644
index 0000000000..223f5dcbde
--- /dev/null
+++ b/hibernate5/src/main/java/com/baeldung/hibernate/pojo/PointEntity.java
@@ -0,0 +1,41 @@
+package com.baeldung.hibernate.pojo;
+
+import com.vividsolutions.jts.geom.Point;
+
+import javax.persistence.Entity;
+import javax.persistence.GeneratedValue;
+import javax.persistence.Id;
+
+@Entity
+public class PointEntity {
+
+ @Id
+ @GeneratedValue
+ private Long id;
+
+ private Point point;
+
+ public PointEntity() {
+ }
+
+ public Long getId() {
+ return id;
+ }
+
+ public void setId(Long id) {
+ this.id = id;
+ }
+
+ public Point getPoint() {
+ return point;
+ }
+
+ public void setPoint(Point point) {
+ this.point = point;
+ }
+
+ @Override
+ public String toString() {
+ return "PointEntity{" + "id=" + id + ", point=" + point + '}';
+ }
+}
diff --git a/hibernate5/src/test/java/com/baeldung/hibernate/HibernateSpatialTest.java b/hibernate5/src/test/java/com/baeldung/hibernate/HibernateSpatialTest.java
new file mode 100644
index 0000000000..6d0aa0a4cd
--- /dev/null
+++ b/hibernate5/src/test/java/com/baeldung/hibernate/HibernateSpatialTest.java
@@ -0,0 +1,97 @@
+package com.baeldung.hibernate;
+
+import com.baeldung.hibernate.pojo.PointEntity;
+import com.vividsolutions.jts.geom.Geometry;
+import com.vividsolutions.jts.geom.Point;
+import com.vividsolutions.jts.io.ParseException;
+import com.vividsolutions.jts.io.WKTReader;
+import org.hibernate.Session;
+import org.hibernate.Transaction;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+
+import javax.persistence.Query;
+import java.io.IOException;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+
+public class HibernateSpatialTest {
+
+ private Session session;
+ private Transaction transaction;
+
+ @Before
+ public void setUp() throws IOException {
+ session = HibernateUtil.getSessionFactory("hibernate-spatial.properties")
+ .openSession();
+ transaction = session.beginTransaction();
+ }
+
+ @After
+ public void tearDown() {
+ transaction.rollback();
+ session.close();
+ }
+
+ @Test
+ public void shouldConvertWktToGeometry() throws ParseException {
+ Geometry geometry = wktToGeometry("POINT (2 5)");
+ assertEquals("Point", geometry.getGeometryType());
+ assertTrue(geometry instanceof Point);
+ }
+
+ @Test
+ public void shouldInsertAndSelectPoints() throws ParseException {
+ PointEntity entity = new PointEntity();
+ entity.setPoint((Point) wktToGeometry("POINT (1 1)"));
+
+ session.persist(entity);
+ PointEntity fromDb = session.find(PointEntity.class, entity.getId());
+ assertEquals("POINT (1 1)", fromDb.getPoint().toString());
+ }
+
+ @Test
+ public void shouldSelectDisjointPoints() throws ParseException {
+ insertPoint("POINT (1 2)");
+ insertPoint("POINT (3 4)");
+ insertPoint("POINT (5 6)");
+
+ Point point = (Point) wktToGeometry("POINT (3 4)");
+ Query query = session.createQuery("select p from PointEntity p "
+ + "where disjoint(p.point, :point) = true", PointEntity.class);
+ query.setParameter("point", point);
+ assertEquals("POINT (1 2)", ((PointEntity) query.getResultList().get(0)).getPoint().toString());
+ assertEquals("POINT (5 6)", ((PointEntity) query.getResultList().get(1)).getPoint().toString());
+ }
+
+ @Test
+ public void shouldSelectAllPointsWithinPolygon() throws ParseException {
+ insertPoint("POINT (1 1)");
+ insertPoint("POINT (1 2)");
+ insertPoint("POINT (3 4)");
+ insertPoint("POINT (5 6)");
+
+ Query query = session.createQuery("select p from PointEntity p where within(p.point, :area) = true",
+ PointEntity.class);
+ query.setParameter("area", wktToGeometry("POLYGON((0 0, 0 5, 5 5, 5 0, 0 0))"));
+ assertEquals(3, query.getResultList().size());
+ assertEquals("POINT (1 1)", ((PointEntity) query.getResultList().get(0)).getPoint().toString());
+ assertEquals("POINT (1 2)", ((PointEntity) query.getResultList().get(1)).getPoint().toString());
+ assertEquals("POINT (3 4)", ((PointEntity) query.getResultList().get(2)).getPoint().toString());
+ }
+
+ private void insertPoint(String point) throws ParseException {
+ PointEntity entity = new PointEntity();
+ entity.setPoint((Point) wktToGeometry(point));
+ session.persist(entity);
+ }
+
+ private Geometry wktToGeometry(String wellKnownText) throws ParseException {
+ WKTReader fromText = new WKTReader();
+ Geometry geom = null;
+ geom = fromText.read(wellKnownText);
+ return geom;
+ }
+}
diff --git a/hibernate5/src/test/resources/hibernate-spatial.properties b/hibernate5/src/test/resources/hibernate-spatial.properties
new file mode 100644
index 0000000000..e85cd49cc3
--- /dev/null
+++ b/hibernate5/src/test/resources/hibernate-spatial.properties
@@ -0,0 +1,10 @@
+hibernate.dialect=org.hibernate.spatial.dialect.mysql.MySQL56SpatialDialect
+hibernate.connection.driver_class=com.mysql.jdbc.Driver
+hibernate.connection.url=jdbc:mysql://localhost:3306/hibernate-spatial
+hibernate.connection.username=root
+hibernate.connection.password=pass
+hibernate.connection.pool_size=5
+hibernate.show_sql=true
+hibernate.format_sql=true
+hibernate.max_fetch_depth=5
+hibernate.hbm2ddl.auto=create-drop
\ No newline at end of file