hibernate-spatial jpa 距离查询

Posted

技术标签:

【中文标题】hibernate-spatial jpa 距离查询【英文标题】:hibernate-spatial jpa distance query 【发布时间】: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
"

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

【问题讨论】:

No data type for node: org.hibernate.hql.internal.ast.tree.IdentNode HQL的可能重复 那么您希望 Hibernate 如何将您的 Point 映射到数据库?对应的列是什么? 【参考方案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();

祝你好运!

【讨论】:

以上是关于hibernate-spatial jpa 距离查询的主要内容,如果未能解决你的问题,请参考以下文章

springboot jpa 分页查询(增删改查)总结

如何从按距离排序的 JPA 实体中获取结果?

springboot整合JPA+MYSQL+queryDSL数据增删改查

springboot整合JPA+MYSQL+queryDSL数据增删改查

springboot整合JPA+MYSQL+queryDSL数据增删改查

Spring Boot(十五):spring boot+jpa+thymeleaf增删改查示例