HQL 意外的 AST 节点:{矢量}

2022-09-02 14:25:06

我正在尝试编写一个HQL查询,以从特许经营商列表中获取属于特定组织的用户列表或任何特许经营商,但是休眠无法解析它。我不知道为什么。以下是 HQL:

from User u where 
(u.parentOrganisation = :topLevelOrganisation or u.parentOrganisation in :franchisees) 
and u.parentOrganisation.deleted = false 
and u.active = true

这是休眠吐出的错误:

unexpected AST node: {vector} [from com.myapp.User u where (u.parentOrganisation = :topLevelOrganisation or u.parentOrganisation in :franchisees0_, :franchisees
1_, :franchisees2_) and u.parentOrganisation.deleted = false and u.active = true]. Stacktrace follows:
Message: unexpected AST node: {vector} [from com.myapp.User u where (u.parentOrganisation = :topLevelOrganisation or u.parentOrganisation in :franchisees0_, :fr
anchisees1_, :franchisees2_) and u.parentOrganisation.deleted = false and u.active = true]

如果我去掉位,所以我的查询看起来像这样:or u.parentOrganisation in :franchisees

from User u where 
(u.parentOrganisation = :topLevelOrganisation) 
and u.parentOrganisation.deleted = false 
and u.active = true

然后它工作正常。我的语法有什么问题?为什么冬眠抱怨这个额外的条款?


答案 1

哦,事实证明我需要用括号括起来::franchisees

from User u where 
(u.parentOrganisation = :topLevelOrganisation or u.parentOrganisation in (:franchisees)) 
and u.parentOrganisation.deleted = false 
and u.active = true

答案 2

发生这种情况的原因是,当数组中的数据放在列表中而不带括号时,从列表中搜索数据库的查询语法将是错误的。

例:

List<Integer> userIdList = [0, 1, 2, 3]

不带括号的查询:from User u where u.id in :list

插入数据时将如下所示 - 语法错误from User u where u.id in 0, 1, 2, 3

带括号的查询:from User u where u.id in (:list)

插入数据时将如下所示 - 正确的语法from User u where u.id in (0, 1, 2, 3)


推荐