views:

46

answers:

1

I have the following class:

class User {
String username;
@CollectionOfElements
private Set<String> roles = new HashSet<String>();
[many more things here]
}

And I want to write a HQL query that retrieves the username and the roles for all the users.

Query query = session.createQuery("select distinct u.username, u.roles from User u");

But it does not work, it throws the following exception:

org.hibernate.QueryException: not an entity [select distinct u.username,u.roles from com.eyeprevent.domain.users.User u]

Complaining that u.roles is not an Entity.

How can I achieve what I want? (Querying where u.roles='arole' would be possible as well but again, it does not work).

+1  A: 

Are you looking for this:

select u.username 
  from User u
 where :role in elements(u.roles)
Pascal Thivent
Maybe that could work. I'll try it on Monday. Thanks.How can I select the roles of an user? Maybe `select u.username, elements(u.roles) from User u`?
pakore