-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: HQL can't handle elements in set mapped by key
PostPosted: Mon Jun 02, 2008 11:48 am 
Newbie

Joined: Mon Jun 02, 2008 11:04 am
Posts: 2
Using Hibernate 3.2.5

We have entities User, Role, Group. The User class has a Map of Roles to sets of Groups. Since the value class of a map must be a mapped entity itself, we created another mapped entity "GroupSet".

class User {
private Map<Role,GroupSet> groups;
}

class GroupSet {
private Set<Group> groups;
}

So now - how to find the users for a given group? These relationships are unidirectional (User -> groups, GroupSet -> groups). So we're going to do an HQL query:

public List<User> getGroupMembers(Group group, Role role) {
String hql =

"from User user where ? in elements(user.groups[?].groups)";

return getHibernateTemplate().find(hql, new Object[] {group, role });
}


Very nice! Except it doesn't work :( Evidently the HQL parser cannot handle the above:

org.hibernate.hql.ast.QuerySyntaxException: expecting CLOSE, found '[' near line 1, column 78 [from User user where ? in elements(user.groups[?].groups)]

Now I know this expression is being interpreted correctly, I can do things like this:


"from User user where user.groups[?].groups.size > 1"

and that runs just fine...

So - is this a bug in Hibernate? Shouldn't I be able to pass the elements function an expression like user.groups[?].groups ??

If not, what is the work-around here?

Kind regards :)

Ossie


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 02, 2008 4:51 pm 
Newbie

Joined: Mon Jun 02, 2008 11:04 am
Posts: 2
Yeah the mappings are straightforward enough:

from User.hbm.xml:

<map name="groups" table="USER_GROUPS" lazy="false" cascade="all">
<key column="USER_ID"/>
<map-key-many-to-many column="ROLE_ID" class="Role"/>
<many-to-many column="GROUP_SET_ID"class="GroupSet"/>
</map>

from GroupSet.hbm.xml:

<set name="groups" table="GROUP_SET_GROUPS" lazy="false">
<key column="GROUP_SET_ID"/>
<many-to-many column="GROUP_ID" class="Group"/>
</set>

And again, the query *seems* straightfoward:

from User user where ? in elements(user.groups[?].groups)

I realize the [] notation is restricted to WHERE clauses, but that's where it is!

Is there really nobody out there with some insight on this one?

Cheers!

Ossie


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.