8

I'm trying to query spatial relations between my entities but keep getting this exception:

org.hibernate.hql.internal.ast.QuerySyntaxException: unexpected AST node: ( near line 1,

My entities are working properly, so is the mapping. I believe that my query has the issue:

SELECT r FROM Regiao r, Imovel i WHERE r.nivel = :nivel AND contains(r.regiao, i.latlng)

Where both r.regiao and i.latln are GeometryType mapped (one being a Polygon and the other one a Point.

Ps.: As I asked the question I finally understood the issue.

Moa
  • 441
  • 4
  • 13

2 Answers2

22

Apparently the syntax of spatial functions requires you to test it against boolean values so I had to add the comparison in the end of my query:

SELECT r FROM Regiao r, Imovel i WHERE r.nivel = :nivel AND contains(r.regiao, i.latlng) = TRUE
Moa
  • 441
  • 4
  • 13
  • while using = true works, I have found it to cause a quite big performance penalty. The generated native query will be ST_CONTAINS(geometry, location)=1 which has quite bad performance compared to just ST_CONTAINS(geometry, location). In my case, having a search on 4 milion row table, this difference is as big as a query taking 2 minutes instead of half a second, just because of one extra =1 at the end of ST_CONTAINS – andrei Apr 13 '22 at 21:04
1

For me this didn't seem to solve a similar problem. I reverted to using Criteria with a SpatialRestriction to solve my problem. This example searches all Regions that contain a given location where location here is a Geometry.

Criteria criteria = getCurrentSession().createCriteria(Region.class);
criteria.add(SpatialRestrictions.contains("theGeom", location));
Tim
  • 41,901
  • 18
  • 127
  • 145
gorik
  • 13
  • 2