Thomas K
Thomas K

Reputation: 123

How to translate this MySQL-Query into a Hibernate-Query

I'd like to ask how to translate this MySQL query into a Hibernate query:

SELECT * FROM Cities WHERE WITHIN(GeomFromText('POINT(52.5 13.3)'), polygon);

or this

SELECT * FROM Cities WHERE MBRContains(polygon, GeomFromText('POINT(52.5 13.3)');

Thank you. Thomas

Upvotes: 4

Views: 957

Answers (1)

Thomas K
Thomas K

Reputation: 123

Got it:

import com.vividsolutions.jts.geom.Coordinate;
import com.vividsolutions.jts.geom.GeometryFactory;
import com.vividsolutions.jts.geom.Point;
import javax.persistence.Query;

Coordinate coord = new Coordinate(52.5, 13.3);
Point point = new GeometryFactory().createPoint(coord);
Query query = em.createQuery("FROM Cities WHERE WITHIN(:location, polygon) = true", Cities.class);
query.setParameter("location", point);

Upvotes: 6

Related Questions