title: ClassCastException while using GROUP BY clause in JPA
body: <p>I am working with JPA. while doing GROUP BY clause example it's throwing ClassCastException.</p>
  
  <p>Below Is My code:</p>
  
  <pre><code>public class StudentGrouping 
  {
  public static void main(String[] args)
  {
      EntityManager entityManager = EntityManagerUtil.getEmf()
              .createEntityManager();
      try {
              EntityTransaction entr = entityManager.getTransaction();
              entr.begin();
              Query query = entityManager
                      .createQuery("SELECT student.studentName, SUM(student.studentAge) FROM Student student GROUP BY student.studentName");
              List&lt;?&gt; list = query.getResultList();
              Iterator&lt;?&gt; iterator = list.iterator();
              while (iterator.hasNext())
              {
                  System.out.println("entered into loop");
                  Student student = (Student) iterator.next();
                  System.out.print("Student Name:"+student.getStudentName());
                  System.out.print(" Age:"+ student.getStudentAge());
                  System.out.println();
  
          }
          entr.commit();
          System.out.println("success");
      } 
      catch (Exception e)
      {
          e.printStackTrace();
      } finally {
          entityManager.close();
      }
  
  }
  
  }
  </code></pre>
  
  <p>Below is The Expection:</p>
  
  <pre><code>java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to com.demo.entities.Student
  at com.demo.action.StudentGrouping.main(StudentGrouping.java:28)
  </code></pre>
  
  <p>My POJO class Fields are:</p>
  
  <pre><code>@Column(name = "studentName")
  private String studentName;
  @Column(name = "studentAge")
  private int studentAge;
  </code></pre>
  
  <p>Is my  GROUP BY clause query is wrong.  </p>