Reputation: 113
I am trying to fetch data from db and mapping it to different entity but I get
java.lang.IllegalArgumentException: java.lang.ArrayIndexOutOfBoundsException: 0
My Table looks like
@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
}
My BO Looks like
@Entity
public class Generic{
@Id
private Long id;
private String code;
private String user_id;
//getters and setters
}
My DAO call class something like this
Query query = entityManager.createNamedQuery("findAll", Generic.class);
query.getResultList();
I get exception on
entityManager.createNamedQuery("findAll", Generic.class);
This is the stack trace I have
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)
Upvotes: 4
Views: 1120
Reputation: 4071
I am trying to fetch data from db and mapping it to a different entity but I get
ArrayIndexOutOfBoundsException
But in the following line, you are not doing that, you are trying to get the list of Student
entity list. And what's your different Entity here?
entityManager.createNamedQuery("findAll", Student.class);
But as you provided another entity Generic
, I assume you want your data to be loaded in that. There are different possible solutions to this problem. Lets figure out.
Update your Native query to this
@NamedNativeQueries({
@NamedNativeQuery(name = "Student.findAll", query = "SELECT NEW Generic(a.student_id, a.student_code) FROM Student a")
})
You have to define a Constructor in the Generic
class as well that qualifies this call
public void Generic(Long id, String code) {
this.id = id;
this.code = code;
}
And update the query execution in DAO as
List<Generic> results = em.createNamedQuery("Student.findAll" , Generic.class).getResultList();
Note: You may have to place the fully qualified path for the Generic
class in the Query for this
List<Object[]>
Alternatively the straight forward and simplest solution would fetch the result list as a list of Object[]
and populate to any new Object you want.
List<Object[]> list = em.createQuery("SELECT s.student_id, s.student_code FROM Student s")
.getResultList();
for (Object[] obj : list){
Generic generic = new Generic();
generic.setId(((BigDecimal)obj[0]).longValue());
generic.setCode((String)obj[1]);
}
Upvotes: 2