Scalar Query Collections in HQL

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 an HQL query that retrieves the username and roles for all 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]

Complains that u.rolesit is not an entity.

How can I achieve what I want? (The request where u.roles='arole'will be possible, but again, it does not work).

+3
source share
1 answer

You are looking for:

select u.username 
  from User u
 where :role in elements(u.roles)
+2
source

Source: https://habr.com/ru/post/1758367/


All Articles