Search in sources :

Example 1 with PointEntity

use of com.baeldung.hibernate.pojo.PointEntity in project tutorials by eugenp.

the class HibernateSpatialTest method shouldSelectAllPointsWithinRadius.

@Test
public void shouldSelectAllPointsWithinRadius() 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, :circle) = true", PointEntity.class);
    query.setParameter("circle", createCircle(0.0, 0.0, 5));
    assertThat(query.getResultList().stream().map(p -> ((PointEntity) p).getPoint().toString())).containsOnly("POINT (1 1)", "POINT (1 2)");
}
Also used : Query(javax.persistence.Query) PointEntity(com.baeldung.hibernate.pojo.PointEntity) Test(org.junit.Test)

Example 2 with PointEntity

use of com.baeldung.hibernate.pojo.PointEntity in project tutorials by eugenp.

the class HibernateSpatialTest method insertPoint.

private void insertPoint(String point) throws ParseException {
    PointEntity entity = new PointEntity();
    entity.setPoint((Point) wktToGeometry(point));
    session.persist(entity);
}
Also used : PointEntity(com.baeldung.hibernate.pojo.PointEntity)

Example 3 with PointEntity

use of com.baeldung.hibernate.pojo.PointEntity in project tutorials by eugenp.

the class HibernateSpatialTest method shouldSelectAllPointsWithinPolygon.

@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))"));
    assertThat(query.getResultList().stream().map(p -> ((PointEntity) p).getPoint().toString())).containsOnly("POINT (1 1)", "POINT (1 2)", "POINT (3 4)");
}
Also used : Query(javax.persistence.Query) PointEntity(com.baeldung.hibernate.pojo.PointEntity) Test(org.junit.Test)

Example 4 with PointEntity

use of com.baeldung.hibernate.pojo.PointEntity in project tutorials by eugenp.

the class HibernateSpatialTest method shouldInsertAndSelectPoints.

@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());
}
Also used : PointEntity(com.baeldung.hibernate.pojo.PointEntity) Test(org.junit.Test)

Example 5 with PointEntity

use of com.baeldung.hibernate.pojo.PointEntity in project tutorials by eugenp.

the class HibernateSpatialTest method shouldSelectDisjointPoints.

@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());
}
Also used : Query(javax.persistence.Query) Point(com.vividsolutions.jts.geom.Point) PointEntity(com.baeldung.hibernate.pojo.PointEntity) Test(org.junit.Test)

Aggregations

PointEntity (com.baeldung.hibernate.pojo.PointEntity)5 Test (org.junit.Test)4 Query (javax.persistence.Query)3 Point (com.vividsolutions.jts.geom.Point)1