【问题标题】:hibernate-spatial jpa distance queryhibernate-spatial jpa 距离查询
【发布时间】:2015-09-18 14:19:22
【问题描述】:

我正在尝试从我的 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}
"}}

任何线索都非常感谢:))) 提前致谢!

【问题讨论】:

标签: java hibernate jpa hibernate-spatial


【解决方案1】:

首先,检查hibernate.dialect 是否设置为persistence.xml 中适当的HS 方言。

其次,尝试使用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();

祝你好运!

【讨论】:

    猜你喜欢
    • 2011-03-03
    • 2015-04-27
    • 1970-01-01
    • 2013-01-04
    • 1970-01-01
    • 2013-09-20
    • 2015-03-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多