Search code examples
javahibernatejdbchql

How to query ElementCollection of String in HQL


I have class like this

public User{
   Long id;
   Set<String> roles;
}

How do I query all User objects with the role of "ADMIN"

EDIT:

I'm using Hibernate 3.0.5. And have tried most of the obvious approaches.

  • from Users where roles in('ADMIN') gives a JDBC error.
  • from Users u where u.roles in('ADMIN') gives a class cast exception

I think this may be a problem with this particular version of Hibernate.


Solution

  • I've found solution:

    "from User as user where 'ADMIN' in elements(user.roles)";
    

    Somehow hql function value() have to help with this, you can also experiment with it, but that hql query above works for me.