简体   繁体   中英

Hibernate spatial query exception, but in the postgres console is working

I'm trying to execute this query in my Java code, using Hibernate and Hibernate Spatial:

Query q = s.createQuery("SELECT c FROM crimes c WHERE ST_DWITHIN(ST_MakeLine(ARRAY['SRID=4326;POINT(-49.30621000000001 -25.515020000000003)','SRID=4326;POINT(-49.30619 -25.515770000000003)','SRID=4326;POINT(-49.306180000000005 -25.5162)','SRID=4326;POINT(-49.305780000000006 -25.5162)']), c.location, 0.0001) = true;");

But, this query causes an Exception:

e = (org.hibernate.hql.internal.ast.QuerySyntaxException) org.hibernate.hql.internal.ast.QuerySyntaxException: expecting CLOSE_BRACKET, found ',' near line 1, column 151 [SELECT c FROM com.safecity.server.db.model.EntityCrime c WHERE ST_DWITHIN(ST_MakeLine(ARRAY['SRID=4326;POINT(-49.305820000000004 -25.515330000000002)','SRID=4326;POINT(-49.306200000000004 -25.515340000000002)','SRID=4326;POINT(-49.30619 -25.515770000000003)','SRID=4326;POINT(-49.306180000000005 -25.5162)','SRID=4326;POINT(-49.305780000000006 -25.5162)']), c.location, 0.0001) = true]

I checked the query, and I cannot find the error. But, if I get this same query and execute on postgres console, the query is executed without any error and returns the correct value.

Please, someone can help me? Thanks.

You are using native query here in hibernate. For this you have to use the query as below:

Query q = s.createSQLQuery("SELECT c FROM crimes c WHERE ST_DWITHIN(ST_MakeLine(ARRAY['SRID=4326;POINT(-49.30621000000001 -25.515020000000003)','SRID=4326;POINT(-49.30619 -25.515770000000003)','SRID=4326;POINT(-49.306180000000005 -25.5162)','SRID=4326;POINT(-49.305780000000006 -25.5162)']), c.location, 0.0001) = true;");

Use createSQLQuery() instead of createQuery() , if you want to create a db native query instead of HQL.

I solved this problem changing the query, for this one:

Query q = s.createQuery("SELECT c FROM crimes c WHERE ST_DWITHIN(ST_GeomFromText('LINESTRING(-49.305820000000004 -25.515330000000002,-49.306200000000004 -25.515340000000002,-49.30619 -25.515770000000003,-49.306180000000005 -25.5162,-49.305780000000006 -25.5162)', 4326), c.location, 0.0001) = true");

I don't know why, but it works.

Thanks for the help.

I have seen similar problems with ARRAY constructors in Hibernate before.

Replace with an array literal (and optionally a type cast) to make it work. A simple string literal will avoid various complications in the communication.

You are using the PostGis function ST_MakeLine() taking an array of geometry as input parameter.

geometry ST_MakeLine ( geometry[] geoms_array )

So:

Query q = s.createQuery(
  "SELECT c FROM crimes c
   WHERE  ST_DWITHIN(ST_MakeLine('{SRID=4326;POINT(-49.30621000000001 -25.515020000000003)
                                  ,SRID=4326;POINT(-49.30619 -25.515770000000003)
                                  ,SRID=4326;POINT(-49.306180000000005 -25.5162)
                                  ,SRID=4326;POINT(-49.305780000000006 -25.5162)}'::geometry[]), c.location, 0.0001)");

This would also explain why your alternative answer providing a linestring (as string literal!) works as well.

Also simplified the boolean expression in the WHERE clause like I commented . Appending = true is just noise.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM