【问题标题】:HQL QuerySyntaxExceptionHQL QuerySyntaxException
【发布时间】:2018-07-19 23:10:13
【问题描述】:

我是一位经验丰富的 SQL 开发人员,刚接触 HQL,我的任务是在 HQL 中编写以下内容并收到以下错误,它不喜欢 CASE 语句的某些内容,但我不知道为什么。 SQL 查询工作正常,但已指示必须使用 HQL。可以的话请帮忙

原因:org.hibernate.hql.internal.ast.QuerySyntaxException:意外的 AST 节点:第 1 行附近的案例,第 143 列 [从 FuelStation 中选择 fs 作为 fs 加入 fs.address 作为 fs.marketRegion = :marketRegion 和...

查询:

String hql = "Select fs from FuelStation as fs join fs.address as a where fs.marketRegion = :marketRegion "
+ "and (  (case when  :phone1 is not null "
+ "         then (    a.phone1 is not null "
+ "               and upper(translate(:phone1,  ' ()+-/@&''.,;$','')) = "
+ "                   upper(translate(a.phone1, ' ()+-/@&''.,;$','')) "
+ "               and (   (    :fsloc is not null "
+ "                        and :fsname is not null "
+ "                        and a.locality is not null "
+ "                        and fs.stationName is not null "
+ "               add upper(translate(:fsloc,         ' ()+-/@&''.,;$','')) = "
+ "                   upper(translate(a.locality,     ' ()+-/@&''.,;$','')) "
+ "               and upper(translate(:fsname,        ' ()+-/@&''.,;$','')) = "
+ "                   upper(translate(fs.stationName, ' ()+-/@&''.,;$',''))) "
+ "                    or (    a.zip is not null "
+ "                        and :fszip is not null "
+ "                        and :fsaddr is not null "
+ "                        and a.address is not null "
+ "                        and upper(translate(:zip,  ' ()+-/@&''.,;$','')) = "
+ "             and upper(translate(a.zip,            ' ()+-/@&''.,;$','')) "
+ "             and upper(translate(:fsaddr,' 0123456789()+-/@&''.,;$','')) = "
+ "             upper(translate(a.address,  ' 0123456789()+-/@&''.,;$',''))))) "
+ "         else (    a.zip is not null "
+ "              and :fszip is not null "
+ "              and :fsloc is not null "
+ "              and :fsname is not null "
+ "              and :fsaddr is not null "
+ "              and a.address is not null "
+ "              and a.locality is not null "
+ "              and fs.stationName is not null "
+ "              and upper(translate(a.zip,          ' ()+-/@&''.,;$','')) = "
+ "                  upper(translate(:fszip,         ' ()+-/@&''.,;$','')) "
+ "              and upper(translate(:fsloc,         ' ()+-/@&''.,;$','')) = "
+ "                  upper(translate(a.locality,     ' ()+-/@&''.,;$','')) "
+ "              and upper(translate(:fsname,        ' ()+-/@&''.,;$','')) = "
+ "                  upper(translate(fs.stationName, ' ()+-/@&''.,;$','')) "
+ "            and upper(translate(:fsaddr,  ' 0123456789()+-/@&''.,;$','')) = "
+ "                upper(translate(a.address,' 0123456789()+-/@&''.,;$',''))) "
+ "         end) "
+ "      or "
+ "        (     abs(abs(a.latitude)  - abs(:lat)) <= 0.001 "
+ "          and abs(abs(a.longitude) - abs(:lng)) <= 0.001)) ";

【问题讨论】:

  • 这是一个海量查询。减少案例逻辑直到它开始工作,然后再添加东西直到它中断。
  • 是的,好的建议问题最终很简单 CASE 语句很好问题是我选择了表列“a.address”而不是类字段“a.addressLine”。我确定是新手 hql 问题
  • 我不确定 HQL 与它本身有多大关系。对于如此复杂的查询,出错的可能性相当高。

标签: hibernate hql


【解决方案1】:

@伯纳德; Hive 无法解释;要么 。或者,在翻译函数中。相反,请尝试在特殊字符之前添加转义字符 \。希望能帮助到你。谢谢。

Old: upper(translate(:phone1,  ' ()+-/@&''.,;$',''))
New: upper(translate(:phone1,  ' ()+-/@&''\.\,\;$',''))

我正在使用 Apache Hive(1.2 版)

sample: select upper(translate('1+2\;)3(456',  ' ()+-/@&''\.\,\;$',''));
result: 123456

【讨论】:

  • @BernardCraddock;让我知道它是否有效。谢谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-05-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-24
相关资源
最近更新 更多