0

I want to get value from the database, in my case I use List to get the value from the database but I got this error

ex = (java.lang.ClassCastException) java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to com.gates.cloud.model.StaffAttendanceTemp

This is my list :

List<StaffAttendanceTemp> attendanceTempList = ds.doQuery("SELECT distinct a.staf.stafPK.kod, a.attendancedate FROM StaffAttendanceTemp a"
    + " WHERE a.staf.stafPK.companyid = :companyid")
        .setParameter("companyid", this.getSession().getCompanyid()).getResultList();

System.out.println(attendanceTempList.getClass().getSimpleName());

List<StaffAttendanceTemp> attendanceTempList2 = ds.doQuery("SELECT a FROM StaffAttendanceTemp a"
    + " WHERE a.staf.stafPK.companyid = :companyid")
        .setParameter("companyid", this.getSession().getCompanyid()).getResultList();

System.out.println(attendanceTempList2.getClass().getSimpleName());

and this is my loop

for (StaffAttendanceTemp attendanceTemprecursive : attendanceTempList) {
    Date timePunchin = null;
    Date timeBreakout = null;
    Date timeBreakin = null;
    Date timePunchout = null;
    Date tarikhPunch = null;

    for (StaffAttendanceTemp attendanceTemp2recursive : attendanceTempList2) {
    }
}

how to solve this?

Neko
  • 33
  • 4
  • First of all, try to improve your question by telling the details. Please have a look at [help] as well as the [how to ask good questions](http://stackoverflow.com/help/how-to-ask) sections for more information on how to improve your question and increase your chances of getting decent help. – Hovercraft Full Of Eels Aug 09 '16 at 02:36
  • Can add one more method? The `ds` method – FreezY Aug 09 '16 at 02:39

1 Answers1

2

The problem come from here :

List<StaffAttendanceTemp> attendanceTempList = ds.doQuery("SELECT distinct a.staf.stafPK.kod, a.attendancedate FROM StaffAttendanceTemp a"
    + " WHERE a.staf.stafPK.companyid = :companyid")
        .setParameter("companyid", this.getSession().getCompanyid()).getResultList();

As you can see ,the ds.doQuery() return an object which is in this case returning List<Object> and not List<StaffAttendanceTemp> class. So the error occur because Object class cannot be cast to custom Class without proper step.

General rule is the following:

If select contains single expression and it's an entity, then result is that entity

If select contains single expression and it's a primitive, then result is that primitive

If select contains multiple expressions, then result is Object[] containing the corresponding primitives/entities So, in your case list is a List.

You can refer here :

JPA Entity 1

JPA query result

One of the solution you can try is change the query to this:

List<StaffAttendanceTemp> attendanceTempList = ds.doQuery("SELECT distinct a FROM StaffAttendanceTemp a"
    + " WHERE a.staf.stafPK.companyid = :companyid")
        .setParameter("companyid", this.getSession().getCompanyid()).getResultList();

Or if you really want a specific parameter only,you need to deal with the object like this:

List<Object[]> attendanceTempList = ds.doQuery("SELECT distinct a.staf.stafPK.kod, a.attendancedate FROM StaffAttendanceTemp a"
    + " WHERE a.staf.stafPK.companyid = :companyid")
        .setParameter("companyid", this.getSession().getCompanyid()).getResultList();

for (Object[] listContent : attendanceTempList) {
//deal with Object[] here
}
Community
  • 1
  • 1
FreezY
  • 1,641
  • 2
  • 18
  • 31
  • the ds : `public Query doQuery(String query) throws Exception { try { return entityManager.createQuery(query); } catch (Exception e) { throw new Exception(new StringBuilder().append("(804) Error in doQuery : ").append(e.getMessage()).toString()); } }` – Neko Aug 09 '16 at 03:12