Returning spatial data with Hibernate

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!

+4
source share
3 answers

Solved by using Object instead of Point in the constructor for the location parameter and subsequent casting in Point to the constructor body.

0
source

My only guess is that your signature of the Event(Point, Double) event constructor and autoboxing does not work, because you are trying to call it like this: Event(location, distance(location, :requestPoint)) , which argues the resolution for the distance ( dot, double).

I check the function used and returns a double not a double .

You can try with select new Event(location, new Double(distance(location, :requestPoint))) , but you cannot be sure.

It may also happen that a function needs 2 geometries as parameters, but I cannot say if it is correct.

+1
source

I suspect that the main problem is the requestPoint request parameter in the constructor call. To check this, try putting the actual double value there and see if it compiles. For instance:

  Query query = session.createQuery("select new Event(location, 2.0) from "+Event.class.getName()); 

Just to see. If this happens, it may mean that the compiler is trying to evaluate the signature of the constructor before evaluating the type of return from the distance () function. Unfortunately, I cannot offer how to fix this, if so.

0
source

Source: https://habr.com/ru/post/1441105/


All Articles