9

Given the query example here: http://www.hibernatespatial.org/tutorial-hs4.html

Query query = em.createQuery("select e from Event e where within(e.location, :filter) = true", Event.class);
query.setParameter("filter", filter);

Is it possible to rewrite the query using jpa 2 criteria api?( I am unsure how i should deal with the within(e.location, :filter) part.

3 Answers 3

14

I recently work at the exact same problem. My solution is a own Predicate for the within-keyword.

    public class WithinPredicate extends AbstractSimplePredicate implements Serializable {
    private final Expression<Point> matchExpression;
    private final Expression<Geometry> area;

    public WithinPredicate(CriteriaBuilderImpl criteriaBuilder, Expression<Point> matchExpression, Geometry area) {
        this(criteriaBuilder, matchExpression, new LiteralExpression<Geometry>(criteriaBuilder, area));
    }
    public WithinPredicate(CriteriaBuilderImpl criteriaBuilder, Expression<Point> matchExpression, Expression<Geometry> area) {
        super(criteriaBuilder);
        this.matchExpression = matchExpression;
        this.area = area;
    }

    public Expression<Point> getMatchExpression() {
        return matchExpression;
    }

    public Expression<Geometry> getArea() {
        return area;
    }

    public void registerParameters(ParameterRegistry registry) {
        // Nothing to register
    }

    @Override
    public String render(boolean isNegated, RenderingContext renderingContext) {
        StringBuilder buffer = new StringBuilder();
        buffer.append(" within(")
                .append(((Renderable) getMatchExpression()).render(renderingContext))
                .append(", ")
                .append(((Renderable) getArea()).render(renderingContext))
                .append(") = true ");
        return buffer.toString();
    }
}

Your query would look like this:

public List<Event> findEventInArea(Geometry area){
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<Event> c = cb.createQuery(Event.class);

    Root<Event> event = c.from(Event.class);
    c.where(new WithinPredicate((CriteriaBuilderImpl) cb, event.get(Event_.location), area));
    Query query = entityManager.createQuery(c);
    return query.getResultList();
}
9

JPA does not support spatial. However, you can unwrap the hibernate session from your JPA EntityManager and run spatial criteria.

The lat lon bounds in this code sample is arbitrary.

@PersistenceContext(unitName = "myPuName")
private EntityManager entityManager;

@Override
public List<City> findCities() {
    CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
    Session session = entityManager.unwrap(Session.class);
    Criteria criteria = session.createCriteria(City.class);
    GeometryFactory geometryFactory = new GeometryFactory();
    Coordinate[] coordinates = {new Coordinate(-9,-9,0),new Coordinate(-9,9,0),new Coordinate(9,9,0),new Coordinate(9,-9,0),new Coordinate(-9,-9,0)};
    LinearRing polygon = geometryFactory.createLinearRing(coordinates);
    Polygon po = geometryFactory.createPolygon(polygon,null);
    criteria.add(SpatialRestrictions.within(City_.location.getName(), po));
    List list = criteria.list();
    return list;
}

Here is some more code not directly related to the question. This class can be used as an "Order" criteria to be added to a hibernate criteria. It will sort results by distance from the argument location:

public class KnnOrder extends Order {
    private final Point fromPoint;

    public KnnOrder(String propertyName, boolean ascending, Point fromPoint) {
        super(propertyName, ascending);
        this.fromPoint = fromPoint;
    }

    @Override
    public String toSqlString(Criteria criteria, CriteriaQuery criteriaQuery) {
        Dialect dialect = criteriaQuery.getFactory().getDialect();
        if (!dialect.getClass().isAssignableFrom(PostgisDialect.class)) {
            throw new UnsupportedOperationException("This supports only postgis dialect. Was requested: " + dialect.toString());
        }
//        final String[] columns = criteriaQuery.getColumnsUsingProjection(criteria, super.getPropertyName());
//        String fromPointWkt = WKTWriter.toPoint(fromPoint.getCoordinate());
        return "location <-> st_setsrid(st_makepoint(" + fromPoint.getX() + "," + fromPoint.getY() + "),4326)";
    }
}
1

In JPA2 you can use the function expression builder. No dedicated stuff required anymore. Works for the order expression too.

public List<Event> listThem(Geometry area) {
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<Event> cq = cb.createQuery(Event.class);
    Root<Event> root = cq.from(Event.class);
    ParameterExpression<Geometry> circleParm = cb.parameter(Geometry.class);
    cq.where(cb.isTrue(cb.function("st_within", Boolean.class, 
                                   root.get(Event_.location), circleParm)));
    TypedQuery<Event> tq = em.createQuery(cq);
    tq.setParameter(circleParm, area);
    return tq.getResultList();
}

Small price: The function name is database-dependent. In PostgreSQL the within function is called st_within.

1
  • How do you create Geometry area that is being passed in params?
    – Zahid
    Commented Dec 15, 2020 at 14:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.