HQL unexpected AST node: {vector}

眉间皱痕 提交于 2019-12-08 16:14:22

问题


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:

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:

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:

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?


回答1:


Oh, it turns out that I needed to enclose :franchisees in parentheses:

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



回答2:


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

will look like this when data is inserted from User u where u.id in 0, 1, 2, 3 - WRONG SYNTAX.

Query with parentheses: 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.




回答3:


We can split condition "OR" in HQL to 2 statements.

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) 


来源:https://stackoverflow.com/questions/24551177/hql-unexpected-ast-node-vector

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!