Java HQL 意外 AST 节点:{vector}
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24551177/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
HQL unexpected AST node: {vector}
提问by rcgeorge23
I'm trying to write an HQL query to grab a list of users that belong to a particular organisation, or any franchisee from a list of franchisees, however hibernate isn't able to parse it. I can't figure out why. Here is the 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
This is the error that hibernate spits out:
这是 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]
If I take out the or u.parentOrganisation in :franchisees
bit, so my query looks like this:
如果我取出这个or u.parentOrganisation in :franchisees
位,那么我的查询看起来像这样:
from User u where
(u.parentOrganisation = :topLevelOrganisation)
and u.parentOrganisation.deleted = false
and u.active = true
Then it works fine. What's wrong with my syntax? Why is hibernate complaining about that extra clause?
然后它工作正常。我的语法有什么问题?为什么 hibernate 会抱怨那个额外的条款?
采纳答案by rcgeorge23
Oh, it turns out that I needed to enclose :franchisees
in parentheses:
哦,原来我需要:franchisees
用括号括起来:
from User u where
(u.parentOrganisation = :topLevelOrganisation or u.parentOrganisation in (:franchisees))
and u.parentOrganisation.deleted = false
and u.active = true
回答by KenShin
We can split condition "OR" in HQL to 2 statements.
我们可以将 HQL 中的条件“OR”拆分为 2 个语句。
It works fine.
它工作正常。
from User u where
(u.parentOrganisation = :topLevelOrganisation and u.parentOrganisation.deleted = false
and u.active = true )
or (u.parentOrganisation in (:franchisees) and u.parentOrganisation.deleted = false
and u.active = true)
回答by Jakov Kusi?
The reason why does this happen is because when data in array is put in the list without parentheses, query syntax for searching database from the list will be wrong.
之所以会出现这种情况,是因为当数组中的数据不带括号放入列表时,从列表中搜索数据库的查询语法将是错误的。
EXAMPLE:
例子:
List<Integer> userIdList = [0, 1, 2, 3]
Query without parentheses: from User u where u.id in :list
不带括号的查询: from User u where u.id in :list
will look like this when data is inserted from User u where u.id in 0, 1, 2, 3
- WRONG SYNTAX.
插入数据时将如下所示from User u where u.id in 0, 1, 2, 3
-错误的语法。
Query with parentheses: from User u where u.id in (:list)
带括号的查询: from User u where u.id in (:list)
will look like this when data is inserted from User u where u.id in (0, 1, 2, 3)
- CORRECT SYNTAX.
插入数据时将如下所示from User u where u.id in (0, 1, 2, 3)
- CORRECT SYNTAX。