【发布时间】: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 与它本身有多大关系。对于如此复杂的查询,出错的可能性相当高。