我正在尝试从我的 mysql 数据库查询最近的 10 个热点,这是我的 JPA 实体:
@Entity
public class HotSpot {
@Id
@GeneratedValue
public Long Id;
@Type(type="org.hibernate.spatial.GeometryType")
public Point location;
@Transient
private Double distance;
public HotSpot( Point location ) {
this.location = location;
}
}
这是我尝试查询的方式:
Query query = em.createQuery("SELECT location, distance( location, :p ) FROM HotSpot");
GeometryFactory gf = new GeometryFactory();
Geometry p = gf.createPoint(new Coordinate(longitude, latitude, 0));
query.setParameter("p", p );
List ret = query.getResultList();
这是我的堆栈跟踪:
13:30:55,281 ERROR [org.jboss.as.server] (management-handler-thread - 2) JBAS015870: Deploy of deployment "Netector.war" was rolled back with the following failure message:
{"JBAS014671: Failed services" => {"jboss.persistenceunit.\"Netector.war#forge-default\"" => "org.jboss.msc.service.StartException in service jboss.persistenceunit.\"Netector.war#forge-default\": java.lang.IllegalStateException: No data type for node: org.hibernate.hql.internal.ast.tree.MethodNode
\\-[METHOD_CALL] MethodNode: '('
+-[METHOD_NAME] IdentNode: 'distance' {originalText=distance}
\\-[EXPR_LIST] SqlNode: 'exprList'
+-[DOT] DotNode: 'hotspot0_.`location`' {propertyName=location,dereferenceType=PRIMITIVE,getPropertyPath=location,path=a.location,tableAlias=hotspot0_,className=com.entity.HotSpot,classAlias=a}
| +-[ALIAS_REF] IdentNode: '(hotspot0_.`id`, hotspot0_.`provider`)' {alias=a, className=com.entity.HotSpot, tableAlias=hotspot0_}
| \\-[IDENT] IdentNode: 'location' {originalText=location}
\\-[NAMED_PARAM] ParameterNode: '?' {name=p, expectedType=null}
Caused by: java.lang.IllegalStateException: No data type for node: org.hibernate.hql.internal.ast.tree.MethodNode
\\-[METHOD_CALL] MethodNode: '('
+-[METHOD_NAME] IdentNode: 'distance' {originalText=distance}
\\-[EXPR_LIST] SqlNode: 'exprList'
+-[DOT] DotNode: 'hotspot0_.`location`' {propertyName=location,dereferenceType=PRIMITIVE,getPropertyPath=location,path=a.location,tableAlias=hotspot0_,className=com.entity.HotSpot,classAlias=a}
| +-[ALIAS_REF] IdentNode: '(hotspot0_.`id`, hotspot0_.`provider`)' {alias=a, className=com.entity.HotSpot, tableAlias=hotspot0_}
| \\-[IDENT] IdentNode: 'location' {originalText=location}
\\-[NAMED_PARAM] ParameterNode: '?' {name=p, expectedType=null}
"}}
非常感谢任何线索:))) 预先感谢!
最佳答案
首先,检查 hibernate.dialect
是否在 persistence.xml
中设置为适当的 H-S 方言。
其次,尝试使用 POJO 来获取数据,以便 Hibernate 可以识别目标类型(警告,代码未测试):
public static class DistanceResult{
public final Point location;
public final Double distance;
public DistanceResult(Point location, Double distance){
this.location = location;
this.distance = distance;
}
}
// .....
TypedQuery<DistanceResult> query = em.createQuery(
"SELECT new myPackage.MyClass.DistanceResult(location, distance( location, :p )) FROM HotSpot",
DistanceResult.class);
GeometryFactory gf = new GeometryFactory();
Geometry p = gf.createPoint(new Coordinate(longitude, latitude, 0));
query.setParameter("p", p );
List<DistanceResult> ret = query.getResultList();
祝你好运!!
关于java - hibernate-spatial jpa 距离查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31161089/