title: QueryDsl projection ElementCollection
body: <p>I'm trying to figure out how to do a DTO projection of an Entity with a list of Enums (@ElementCollection). Unfortunately, the QueryDsl Documentation is lacking and here I only find results for version 3 which are <strong>not</strong> applicable for version 4.</p>
  
  <pre><code>@Entity
  public class User {
      private String username;
  
      @Enumerated(EnumType.STRING)
      @ElementCollection(targetClass = Permission.class)
      private Set&lt;Permission&gt; permissions;
  }
  </code></pre>
  
  <p>And I'd like a DTO with a set/list/array of either Permission-Enums or simply Strings (will be converted to JSON anyway). A simple constructor expression doesn't work:</p>
  
  <pre><code>List&lt;UserDto&gt; users = new JPAQueryFactory(eM).select(
              Projections.constructor(UserDto.class,
                      QUser.user.username, QUser.user.permissions))
              .from(QUser.user)
              .fetch();
  </code></pre>
  
  <p>Gives me <code>org.hibernate.QueryException: not an entity</code></p>
  
  <p>All the examples with <code>.transform()</code> I've seen use a groupBy and return a Map. I'm generating these queries dynamically and I want a List of DTOs, not sometimes a List of DTO and sometimes a Map.</p>
  
  <p>EDIT:</p>
  
  <p>Something like that if I were to write a native PostgreSQL query:</p>
  
  <pre><code>select id, username, array_remove(array_agg(up.permissions), null) as permissions
  from users u
  left join users_permissions up on up.uid = u.id
  group by u.id;
  </code></pre>
  
  <p>EDIT 2:</p>
  
  <p>I guess this is what I'd have to do with JPQL? :puke:</p>
  
  <pre><code>List&lt;UserDto&gt; users = (List&lt;UserDto&gt;) eM.getEntityManager().createQuery(
                  "SELECT u.id, u.username, u.tenantId, u.fullname, u.active, u.localeKey, perms " +
                          "FROM User u " +
                          "LEFT JOIN u.permissions perms")
                  .unwrap(org.hibernate.query.Query.class)
                  .setResultTransformer(
                          new ResultTransformer() {
                              private Map&lt;Long, UserDto&gt; res = new HashMap&lt;&gt;();
  
                              @Override
                              public Object transformTuple(Object[] tuple, String[] aliases) {
                                  UserDto u = res.get(tuple[0]);
                                  if (u == null) {
                                      u = new UserDto((Long) tuple[0], (String) tuple[1], "", (String) tuple[2], (String) tuple[3], (boolean) tuple[4], (String) tuple[5], EnumSet.of((Permission) tuple[6]));
                                      res.put(u.getId(), u);
                                  } else {
                                      u.getPermissions().add((Permission) tuple[6]);
                                  }
  
                                  return null;
                              }
  
                              @Override
                              public List&lt;UserDto&gt; transformList(List tuples) {
                                  return new ArrayList&lt;&gt;(res.values());
                              }
                          })
                  .getResultList();
  </code></pre>