rcg*_*e23 9 java grails hibernate hql
我正在尝试编写一个HQL查询来获取属于特定组织的用户列表,或者来自特许经营者列表中的任何特许经营者,但是hibernate无法解析它.我无法弄清楚为什么.这是HQL:
from User u where
(u.parentOrganisation = :topLevelOrganisation or u.parentOrganisation in :franchisees)
and u.parentOrganisation.deleted = false
and u.active = true
Run Code Online (Sandbox Code Playgroud)
这是hibernate吐出的错误:
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]
Run Code Online (Sandbox Code Playgroud)
如果我取出这个or u.parentOrganisation in :franchisees位,那么我的查询看起来像这样:
from User u where
(u.parentOrganisation = :topLevelOrganisation)
and u.parentOrganisation.deleted = false
and u.active = true
Run Code Online (Sandbox Code Playgroud)
然后它工作正常.我的语法有什么问题?为什么hibernate抱怨这个额外的子句?
rcg*_*e23 29
哦,事实证明我需要括:franchisees在括号中:
from User u where
(u.parentOrganisation = :topLevelOrganisation or u.parentOrganisation in (:franchisees))
and u.parentOrganisation.deleted = false
and u.active = true
Run Code Online (Sandbox Code Playgroud)
发生这种情况的原因是,当将数组中的数据放在列表中且没有括号时,用于从列表中搜索数据库的查询语法将是错误的。
例:
List<Integer> userIdList = [0, 1, 2, 3]
Run Code Online (Sandbox Code Playgroud)
查询不带括号: 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)- 正确语法。