title: Hibernate custom DTO with a list field
body: <p>I have 2 entities</p>
  
  <pre><code>@Entity
  public class DeptEmployee {
  
      @Id
      @GeneratedValue(strategy = GenerationType.SEQUENCE)
      private long id;
  
      private String employeeNumber;
  
      private String designation;
  
      private String name;
  
      @ManyToOne
      private Department department;
  
      // constructor, getters and setters 
  }
  </code></pre>
  
  <pre><code>@Entity
  public class Department {
  
      @Id
      @GeneratedValue(strategy = GenerationType.SEQUENCE)
      private long id;
  
      private String name;
  
      @OneToMany(mappedBy="department")
      private List&lt;DeptEmployee&gt; employees;
  
      public Department(String name) {
          this.name = name;
      }
  
      // getters and setters 
  }
  </code></pre>
  
  <p>I know that I can extract the DTO <code>Result</code> like this:</p>
  
  <pre><code>public class Result {
      private String employeeName;
  
      private String departmentName;
  
      public Result(String employeeName, String departmentName) {
          this.employeeName = employeeName;
          this.departmentName = departmentName;
      }
  
      public Result() {
      }
  
      // getters and setters 
  }
  </code></pre>
  
  <pre><code>Query&lt;Result&gt; query = session.createQuery("select new com.baeldung.hibernate.pojo.Result(m.name, m.department.name)"
    + " from com.baeldung.hibernate.entities.DeptEmployee m");
  List&lt;Result&gt; results = query.list();
  </code></pre>
  
  <p>(thanks to the example in this article: <a href="https://www.baeldung.com/hibernate-query-to-custom-class" rel="nofollow noreferrer">https://www.baeldung.com/hibernate-query-to-custom-class</a>)</p>
  
  <p><BR/>
  <BR/>
  Now I would like to extract a DTO which contains a department name and a list of the names of the employees in this department.</p>
  
  <pre><code>public class Result2 {
      private String departmentName;
  
      private List&lt;String&gt; employeeNames;
  
      // Constructor ???
  
      public Result2() {
      }
  
      // getters and setters 
  }
  </code></pre>
  
  <p>My questions are:</p>
  
  <ul>
  <li>is that possible?</li>
  <li>what is the constructor in <code>Result2</code>?</li>
  <li>what is the hql query to extract this <code>Result2</code>?</li>
  </ul>