i want to convert Object to Short. my code is like this :
List<Batch> lsMaxBatch = em.createNativeQuery(sqlMaxBatch).getResultList();
Iterator iter = lsMaxBatch.iterator();
while (iter.hasNext()) {
Short batchId = Short.valueOf(iter.next() + "");
ls.add(batchId);
}
is it okay?
because if i doing convert like this :
Short batchId = (Short) iter.next();
i get an error :
java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to java.lang.Short
thanks
Untyped queries like these return a
List<Object[]>. The exception message hints at this:[Ljava.lang.Object; cannot be cast....To fix your code:
Yes, getting a
List<Object[]>sucks. To improve this, use one of theEntityManagerquery creation methods which returns aTypedQuery<T>.