I am trying to return spatial data (ie Point) from database using Hibernate Spatial library (http://www.hibernatespatial.org/) using this WORKING approach…
...
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;
but I would like to use something like this (use Event class constructor in select statment)…
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;
The problem is that the second approach gives me following exception…
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]
but I cannot see why, because inside of my Event class I have a constructor…
public Event(Point location, Double distance) {
this.location = location;
this.distance = distance;
}
for clarity Event class has following properties…
@Column(name="LOCATION", columnDefinition = "MDSYS.SDO_GEOMETRY", nullable = false)
@Type(type = "org.hibernate.spatial.GeometryType")
private Point location;
private Double distance;
I have checked return class for each column that Hibernate returns and it matches Point and Double. Does anyone has any idea what I am doing wrong? Thanks you!
Solved by using Object instead of Point in constructor for location parameter and casting to Point afterwards in constructor body.