hibernate HQL elements error
Question
My HibernateUserImpl mapping look like below
<set name="groups" table="OS_USER_GROUP" inverse="false" cascade="none" lazy="false">
<key column="user_id"/>
<many-to-many column="group_id" class="com.opensymphony.user.provider.hibernate3.ahxu.impl.HibernateGroupImpl"/>
</set>
I do HQL query like
select distinct hibuser from HibernateUserImpl hibuser, hibuser.groups.elements hibgroup where hibuser.name = ? and hibgroup.name = ?
I get error for hibuser.groups.elements . Is there anything I miss out?
error in console
org.hibernate.hql.ast.QuerySyntaxException: hibuser.groups.elements is not mappe
d [select distinct hibuser from com.opensymphony.user.provider.hibernate3.ahxu.i
mpl.HibernateUserImpl hibuser, hibuser.groups.elements hibgroup where hibuser.n
ame = ? and hibgroup.name = ?]
Solution
I think you need to make an explicit join.
select distinct hibuser
from HibernateUserImpl hibuser
join hibuser.groups hibgroup
where hibuser.name = ? and hibgroup.name = ?
OTHER TIPS
hibuser.groups is a collection, thus you cannot access attribut of the element of the collection.
This can be illustrated like :
List groups = ...; groups.elements <- illegal function access.
Thus to access hibuser.groups.elements, you need to do multiple joins :
select element
from HibernateUserImpl hibuser
join hibuser.groups group
join group.elements element
where hibuser.name = ? and hibgroup.name = ? and element.whatever = something
But here you only select on hibuser, thus you should not join on other collections. Not joining will first give you a faster query, and avoid usage of distinct. Try to avoid distinct as much as possible.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow