title: Convert Hibernate Resultset to a pojo containing a list of pojos
body: <p>I have a pojo similar to this:</p>
  
  <pre><code>StudentResultPojo{
  studentId;
  studentName;
  studentEmail;
  List&lt;CourseResultPojo&gt; coursePojoList;
  }
  </code></pre>
  
  <p>with CourseResultPojo looking like this:</p>
  
  <pre><code>CourseResultPojo{
  name;
  teacher;
  time;
  teacherPhone;
  teacherEmail;
  }
  </code></pre>
  
  <p>I have Hibernate entities (Database : Postgres) for Student, Course, Teacher, Phone, Email etc.</p>
  
  <p>Here is a snippet of code I have:</p>
  
  <pre><code>String query = "SELECT
      distinct student.id as \"studentId\",
      student.name as \"studentName\",
      student.email as \"studentEmail\"
  FROM
      sample.sample_Student student";
  
  Query q = entityManager.unwrap(Session.class).
                  createSQLQuery(query).
  setResultTransformer(Transformers.aliasToBean(StudentResultPojo.class));
          return q.list(); 
  </code></pre>
  
  <p>I would like to keep the query in NativeSQL as opposed to JPQL. 
  Edit: Because the query can change anytime. I would like to store the query in the db and edit it as needed. Testing will be easier if it's in Native query.</p>
  
  <p>I know JPQL has a similar functionality where we can convert results from a JPA query to pojo through it's constructor like select new (Pojo.class.getName()....) but that has the same problem I am facing.</p>
  
  <p>With what I have, I will have to loop through the StudentResultPojo list that my service returns and I will have to fill the coursePojoList for every StudentResultPojo by retrieving the data from the database or in the Java code by navigating through their Hibernate relationships which takes some time. </p>
  
  <p>Entities look like:</p>
  
  <pre><code>Student{
  @Id
  @GeneratedValue(strategy = GenerationType.IDENTITY)
  @Column(name = "id", unique = true, nullable = false)
  private Long id;
  private String name;
  @OneToMany(fetch = FetchType.LAZY, mappedBy = "student")
  private Set&lt;Course&gt; courses = new HashSet&lt;&gt;(0);
  
  }
  
  Course{
  @Id
  @GeneratedValue(strategy = GenerationType.IDENTITY)
  @Column(name = "id", unique = true, nullable = false)
  private Long id;
  private String name;
  private String time;
  @ManyToOne(fetch = FetchType.LAZY)   
  @JoinColumn(name = "student_id", referencedColumnName = "pk")    
  @ForeignKey(name = "student_course")     
  private Student student;
  @OneToOne(fetch = FetchType.LAZY, mappedBy = "course")
  private Set&lt;Teacher&gt; teacher;
  }
  ......
  </code></pre>
  
  <p>Question: If I have a query that returns results in 2 rows (Not JSON, just the results returned when a query is run)</p>
  
  <pre><code>1, James, james.t@yahoo.com, Physics, Albert, 9.00, 7887897899, physics.teacher@yahoo.com
  1, James, james.t@yahoo.com, English, Jackie, 10.00, 7887097899, english.teacher@yahoo.com
  </code></pre>
  
  <p>Is there a way I can transform the results into a single StudentResultPojo with the the list of CourseResultPojos? </p>
  
  <p>Meaning my StudentResultPojo will have the attribute values as</p>
  
  <pre><code>1, James, james.t@yahoo.com for id, name and email respectively
  </code></pre>
  
  <p>and the list will have two CourseResultPojo objects with</p>
  
  <pre><code>1st Object values as Physics, Albert, 9.00, 7887897899, physics.teacher@yahoo.com for attributes name, teacher,    time,teacherPhone,teacherEmail
  
  2nd Object values as English, Jackie, 10.00, 7887097899, english.teacher@yahoo.com for attributes name,teacher,time,teacherPhone,teacherEmail
  </code></pre>
  
  <p>Thank you.</p>