title: How can I map the result set of query to a different entity other than table
body: <p>I am trying to fetch data from db and mapping it to different entity but I get </p>
  
  <pre><code>java.lang.IllegalArgumentException: java.lang.ArrayIndexOutOfBoundsException: 0
  </code></pre>
  
  <p>My Table looks like </p>
  
  <pre><code>@Entity
  @Table(name = "Student")
  @NamedNativeQueries({
      @NamedNativeQuery(name = "findAll", query = "Select a.student_id as id, a.student_code as code from Student a")
      })
  public class Student {
  
      @Id
      private Long student_id;
      private String student_code;
      private String student_user_id;
      private String student_first_name;
  
      //Some other fields, getters and setters
  }
  </code></pre>
  
  <p>My BO Looks like </p>
  
  <pre><code>@Entity
  public class Generic{
  
      @Id
      private Long id;
      private String code;
      private String user_id;
  
      //getters and setters
  }
  
  </code></pre>
  
  <p>My DAO call class something like this</p>
  
  <pre><code>Query query = entityManager.createNamedQuery("findAll", Generic.class);
  query.getResultList();
  </code></pre>
  
  <p>I get exception on</p>
  
  <pre><code>entityManager.createNamedQuery("findAll", Generic.class);
  </code></pre>
  
  <p>This is the stack trace I have </p>
  
  <pre><code>    Caused by: java.lang.IllegalArgumentException: java.lang.ArrayIndexOutOfBoundsException: 0
  at org.hibernate.internal.AbstractSharedSessionContract.buildQueryFromName(AbstractSharedSessionContract.java:774)
  at org.hibernate.internal.AbstractSharedSessionContract.createNamedQuery(AbstractSharedSessionContract.java:869)
  at org.hibernate.internal.AbstractSessionImpl.createNamedQuery(AbstractSessionImpl.java:23)
  at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
  at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
  at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
  at java.lang.reflect.Method.invoke(Method.java:498)
  at org.springframework.orm.jpa.SharedEntityManagerCreator$SharedEntityManagerInvocationHandler.invoke(SharedEntityManagerCreator.java:301)
  at com.sun.proxy.$Proxy157.createNamedQuery(Unknown Source)
  </code></pre>