I used this native query to fetch data.
select c.id,c.name from customer c,information i where i.status=1 and c.id=i.customerId
and i used the below code to convert the object list into integer list as
public List<Integer> getAllIdsByGroupId(Integer groupId) throws Exception {
List<Object[]> list = repository.getAllIdsByGroupId(groupId);
List<Integer> ids = repository.mapIds(list);
return ids;
}
public List<Object[]> getAllIdsByGroupId(Integer groupId) {
Query query = entityManager.createNativeQuery("select c.id,c.name from customer c,information i where i.status=1 and c.id=i.customerId and c.groupId=:groupId");
query.setParameter("groupId", groupId);
List<Object[]> list = query.getResultList();
if (list == null || list.isEmpty()) {
return null;
}
return list;
}
public List<Integer> mapIds(List<Object[]> list) {
List<Integer> ids = new ArrayList<Integer>();
if (list != null && !list.isEmpty()) {
Integer id;
for (Object[] object : list) {
id = (object[0] != null ? (Integer) object[0] : null);
ids.add(id);
}
}
return ids;
}
The query retrieves a list with id and name.and the above code gives output without exception.But i require List with only ids. when i remove c.name from query as follows
select c.id from customer c,information i where i.status=1 and c.id=i.customerId
the above code fails and produce the exception
java.lang.Integer cannot be cast to [Ljava.lang.Object;
Any helps? thanks in advance.
listis already a list ofIntegers.List<Integer>.