Using JPA Criteria Api and hibernate spatial 4 together

别等时光非礼了梦想. 提交于 2019-11-27 23:11:10

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();
}

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)";
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!