|
| 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 | +} |
0 commit comments