我正在尝试使用 Hibernate 空间库 (http://www.hibernatespatial.org/) 使用这种工作方法从数据库返回空间数据(即点)...
...
Session session = sessionFactory.openSession();
Query query = session.createQuery("select location, distance(location, :requestPoint) from "+Event.class.getName());
query.setParameter("requestPoint", requestPoint);
List<?> rows = query.list();
session.close();
List<Event> events = new ArrayList<Event>();
for (Iterator<?> it = rows.iterator(); it.hasNext(); ) {
Object[] row = (Object[]) it.next();
Event event = new Event();
event.setLocation((Point) row[0]);
event.setDistance((Double) row[1]);
events.add(event);
}
return events;
但我想使用这样的东西(在选择语句中使用 Event 类构造函数)...
Session session = sessionFactory.openSession();
Query query = session.createQuery("select new Event(location, distance(location, :requestPoint)) from "+Event.class.getName());
query.setParameter("requestPoint", requestPoint);
List<Event> rows = query.list();
session.close();
return rows;
问题是第二种方法给了我以下异常...
org.hibernate.hql.internal.ast.QuerySyntaxException: Unable to locate appropriate constructor on class [com.jaygridley.aet.Event] [select new Event(location, distance(location, :requestPoint)) from com.jaygridley.aet.domain.Event]
但我不明白为什么,因为在我的 Event 类中我有一个构造函数...
public Event(Point location, Double distance) {
this.location = location;
this.distance = distance;
}
为清楚起见,事件类具有以下属性...
@Column(name="LOCATION", columnDefinition = "MDSYS.SDO_GEOMETRY", nullable = false)
@Type(type = "org.hibernate.spatial.GeometryType")
private Point location;
private Double distance;
我检查了 Hibernate 返回的每一列的返回类,它与 Point 和 Double 匹配。有谁知道我做错了什么?谢谢!
最佳答案
我唯一的猜测是您的 Event 构造函数签名是 Event(Point, Double)
并且自动装箱不起作用,因为您正试图像这样实例化它 Event(location, distance(location , :requestPoint))
args 解析为 distance(Point, double)。
我一直在检查你正在使用的功能,它returns double
不是 Double
。
您可以尝试使用 "select new Event(location, new Double(distance(location, :requestPoint)))
但不能确定。
也可能是这个函数需要2个Geometry作为参数,但是我也不好说对不对。
关于java - 使用 Hibernate 返回空间数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12998937/