objectcastingtypecast-operator

java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to my custom model class


Hello Everyone i am a bit new with hibernate query result and stuff as per my requirements i have to build a json result from following query i have my model class which i will be returning as json response, The query.getResult() returns me with object class but type casting is not working for me is there any alternate way for this.I want to map the query result with my custom Model Object.

 List<Object[]>  result = query.getResultList();
            ListIterator<Object[]> iter = result.listIterator();
            List<ReferFriend> refer=new ArrayList<>();
            while (iter.hasNext()){
                @SuppressWarnings("unchecked")
                ReferFriend referFriend=  (ReferFriend) (Object)iter.next();
                refer.add(referFriend);
            }
            return refer;
        } catch (NoResultException e) {
            return null;
        }
    }

Solution

  • I got this Now.I stored the result as an object array and Iterated over it.I then explicitly casted each element to my pojo class elements.The problem i was facing earlier was the first parameter the query result was big integer and my pojo had Int field. Thanks @rkosegi .