我正在尝试使用 Hibernate-Spatial 中的谓词在 X 英里(用户定义)的半径内进行搜索。
尽管之前已经提出过此类问题,但之前仅使用 Predicates 提出过一次,并且可能不是最新的,因为它不起作用。而且我必须能够使用谓词而不是 HQL。
我通过另一个答案找到了如何为 PostGIS 的 WITHIN 关键字设置谓词,但无法运行查询。
最后设置后,我说明错误输出和包版本。
这是我的架构中的内容:
CREATE EXTENSION Postgis;
CREATE TABLE "user" (
id bigint NOT NULL,
search_radius int,
latitude double precision,
longitude double precision,
location geography(point, 4326),
PRIMARY KEY (id)
);
在我的模型中:
import com.vividsolutions.jts.geom.Point;
import com.vividsolutions.jts.io.ParseException;
import com.vividsolutions.jts.io.WKTReader;
import org.hibernate.annotations.Type;
@Entity
@Table(name = "`user`")
public class User implements UserDetails {
@Id
private Long id;
private Integer searchRadius = 125;
private Double latitude;
private Double longitude;
@Column(name = "location", columnDefinition = "geography(Point, 4326)", nullable = true)
@Type(type = "jts_geometry")
private Point location;
...
private void updateLocation() {
if (latitude != null && longitude != null) {
try {
location = (Point) new WKTReader().read("POINT(" + latitude + " " + longitude + ")");
location.setSRID(Location.SRID);
} catch (ParseException e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
}
}
在 lat / lng 的设置器中调用更新位置。
谓词,我从Using JPA Criteria Api and hibernate spatial 4 together中获得
import com.vividsolutions.jts.geom.Geometry;
import com.vividsolutions.jts.geom.Point;
import java.io.Serializable;
import javax.persistence.criteria.Expression;
import org.hibernate.jpa.criteria.CriteriaBuilderImpl;
import org.hibernate.jpa.criteria.ParameterRegistry;
import org.hibernate.jpa.criteria.Renderable;
import org.hibernate.jpa.criteria.compile.RenderingContext;
import org.hibernate.jpa.criteria.predicate.AbstractSimplePredicate;
public class WithinPredicate extends AbstractSimplePredicate implements Expression<Boolean>, Serializable {
private final Expression<Point> matchExpression;
private final Expression<Geometry> 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 ");
String bo = buffer.toString();
return bo;
}
}
并使用 WithinPredicate 和 circle 工厂创建查询。
import com.xxxx.repository.WithinPredicate;
import com.vividsolutions.jts.geom.Coordinate;
import com.vividsolutions.jts.geom.Geometry;
import com.vividsolutions.jts.util.GeometricShapeFactory;
@Service
class ... {
public List<User> fetchFor(User user) {
...
Geometry radius = createCircle(user.getLatitude(), user.getLongitude(), user.getSearchRadius());
radius.setSRID(Location.SRID);
p = b.and(p, new WithinPredicate(
(CriteriaBuilderImpl) b,
u.get(User_.location),
new LiteralExpression<Geometry>((CriteriaBuilderImpl) b, radius)
));
}
private static Geometry createCircle(double x, double y, final double RADIUS) {
GeometricShapeFactory shapeFactory = new GeometricShapeFactory();
shapeFactory.setNumPoints(32);
shapeFactory.setCentre(new Coordinate(x, y));//there are your coordinates
shapeFactory.setSize(RADIUS * 2);//this is how you set the radius
return shapeFactory.createCircle().getBoundary();
}
}
不幸的是,我在输出中得到的错误是:
2016-09-09 14:03:30.862 WARN 56393 --- [ main] o.h.engine.jdbc.spi.SqlExceptionHelper : SQL Error: 0, SQLState: 42883
2016-09-09 14:03:30.862 ERROR 56393 --- [ main] o.h.engine.jdbc.spi.SqlExceptionHelper : ERROR: function st_within(geography, bytea) does not exist
Hint: No function matches the given name and argument types. You might need to add explicit type casts.
Position: 336
gradle 中的休眠空间版本:
compile('org.hibernate:hibernate-spatial:5.0.9.Final')
我怀疑这个问题可能是由于模型/模式设置为“地理(点,4326)”类型,但 shapeFactory 创建了“几何(点,4326)”类型的半径。
非常感谢您对此的任何帮助。谢谢。