0

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

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

4

1 回答 1

1

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

二、尝试使用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();

祝你好运!!

于 2015-07-02T11:12:50.953 回答