hibernate-spatial jpa距离查询

时间:2015-07-01 12:17:59

标签: java hibernate jpa hibernate-spatial

我试图从我的mysql数据库中查询最近的10个hotsspots,这是我的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}
"}}

任何线索很多人赞赏:))) 提前谢谢!

1 个答案:

答案 0 :(得分:0)

首先,检查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();
祝你好运!!