我正在尝试使用 Hibernate Spatial 库(http://www.hibernatespatial.org/)使用这种 WORKING 方法从数据库中返回空间数据(即点)...
...
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;
但我想使用这样的东西(在选择语句中使用事件类构造函数)......
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。有谁知道我做错了什么?谢谢!