简体   繁体   English

Hibernate 空间查询异常,但在 postgres 控制台中正在工作

[英]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:我正在尝试使用 Hibernate 和 Hibernate Spatial 在我的 Java 代码中执行此查询:

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.但是,如果我得到相同的查询并在 postgres 控制台上执行,则查询执行时没有任何错误并返回正确的值。

Please, someone can help me?拜托,有人可以帮助我吗? Thanks.谢谢。

You are using native query here in hibernate.您在 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.如果要创建数据库本机查询而不是 HQL,请使用createSQLQuery()而不是createQuery()

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.我之前在 Hibernate 中看到过ARRAY 构造函数的类似问题。

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.您正在使用ST_MakeLine()函数ST_MakeLine()geometry数组作为输入参数。

geometry ST_MakeLine ( geometry[] geoms_array )几何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 .像我评论的那样简化了WHERE子句中的布尔表达式。 Appending = true is just noise. Appending = true只是噪音。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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