Hibernate 关于在HQL上使用多个where exists语句的问题
我不得不在GrailsHibernate4上执行一个复杂的查询,所以我决定使用Hibernate 关于在HQL上使用多个where exists语句的问题,hibernate,grails,hql,Hibernate,Grails,Hql,我不得不在GrailsHibernate4上执行一个复杂的查询,所以我决定使用HQL。这是我的查询的简化版本(它已经在运行,没有bug): 我发现的第一个问题是: and lab.rrType in ('SC') group by laa.refNo, laa.amount having sum(b.amount) = laa.amount ) and adviceType = 'C' // A newly
HQL
。这是我的查询的简化版本(它已经在运行,没有bug):
我发现的第一个问题是:
and lab.rrType in ('SC')
group by laa.refNo, laa.amount
having sum(b.amount) = laa.amount
)
and adviceType = 'C' // A newly added condition
当我在HQL
末尾附加另一个简单子句时,它抛出一个异常:
URI://分支/检索分支
类:java.lang.NullPointerException
消息:空
乍一看,它应该是有效的。我已经解决了这个问题,将这个新语句放在普通where语句和exists
语句之间
and agency.id = :agency
and branch = :branch
and str(refNo) like :refNo
and adviceType = 'C' // Placed it here instead on the last line
and not exists (
from AdviceHeader as la, ReportHeader as h
where h.caRefNo is not null
为什么我不能像在普通查询中一样将其添加到where
子句的最后一部分?为什么要加在中间呢
在这个场景中,HQL
的另一个更紧迫的问题是orderby
子句。基于上面的问题,我有点期待,如果在语句末尾添加,它也会抛出一个错误——尽管我已经尝试过了
and lab.rrType in ('SC')
group by laa.refNo, laa.amount
having sum(b.amount) = laa.amount
)
order by refNo asc // I added it obviously on the last part
但正如所料,它也失败了。如果我试着把它放在中间,就像我上面所做的那样,它会犯一个错误,指向错误的<>代码>由< /COD> >子句:
类:QuerySyntaxException
消息:GrailExceptionResolver-处理请求时发生QuerySyntaxException。意外标记
经过一些调试,我发现如果我真的想让order by
子句工作,我应该删除所有那些不存在的子句,这是一个问题。我想指出的是,我的查询工作正常;我唯一缺少的是分类。是否存在
和order by
子句不能在一个HQL
语句上-某种兼容性问题?这是HQL
的已知限制吗?那些不存在的语句是怎么回事,它们一直在窃听这个查询和上面的查询
注意:我已经对所有涉及的域类执行了测试,以确定是否存在明显的错误,例如缺少映射、外键约束、标识约束和不匹配的数据类型。简单的findAll()
和createCriteria()
也可以正常工作。我希望运行的查询是
from AdviceHeader as pa
where adviceType = 'C'
and rrType = 'SC'
and agency.id = :agency
and branch = :branch
and str(refNo) like :refNo
and not exists (
from AdviceHeader as la, ReportHeader as h
where h.caRefNo is not null
and h.caRefNo = la.refNo
and la.refNo2 = pa.refNo
)
and not exists (
from AdviceHeader as laa, AdviceHeader as lab
where laa.refNo = lab.refNo2
and laa.refNo = pa.refNo
and laa.adviceType = 'C'
and laa.statusCode in ('CL', 'PR')
and laa.rrType in ('RS')
and lab.rrType in ('SC')
group by laa.refNo, laa.amount
having sum(b.amount) = laa.amount
)
and adviceType = 'C' // This should work regardless its position on this where clause
order by refNo asc // This should only work ON THE LAST PART of the HQL query
from AdviceHeader as pa
where adviceType = 'C'
and rrType = 'SC'
and agency.id = :agency
and branch = :branch
and str(refNo) like :refNo
and not exists (
from AdviceHeader as la, ReportHeader as h
where h.caRefNo is not null
and h.caRefNo = la.refNo
and la.refNo2 = pa.refNo
)
and not exists (
from AdviceHeader as laa, AdviceHeader as lab
where laa.refNo = lab.refNo2
and laa.refNo = pa.refNo
and laa.adviceType = 'C'
and laa.statusCode in ('CL', 'PR')
and laa.rrType in ('RS')
and lab.rrType in ('SC')
group by laa.refNo, laa.amount
having sum(b.amount) = laa.amount
)
and adviceType = 'C' // This should work regardless its position on this where clause
order by refNo asc // This should only work ON THE LAST PART of the HQL query