I am trying to return spatial data (i.e. Point) from a database using the 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 the event class constructor in selected status) ...
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 the 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 don’t understand why, because inside my Event class I have a constructor ...
public Event(Point location, Double distance) { this.location = location; this.distance = distance; }
for clarity, the Event Class has the following properties ...
@Column(name="LOCATION", columnDefinition = "MDSYS.SDO_GEOMETRY", nullable = false) @Type(type = "org.hibernate.spatial.GeometryType") private Point location; private Double distance;
I checked the return class for each column that Hibernate returns, and it matches Point and Double. Does anyone know what I'm doing wrong? Thank you!