1

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.

9
  • What object list? Is this SQL? A native query from a JPA provider? OrientDB? Commented Jan 31, 2017 at 4:52
  • 1
    Please include a minimal reproducible example. Commented Jan 31, 2017 at 4:52
  • 4
    It looks to me like your list is already a list of Integers. Commented Jan 31, 2017 at 4:53
  • 2
    Good point. Try changing the type to List<Integer>. Commented Jan 31, 2017 at 4:55
  • can u tell the 'list ' variable is in which type Commented Jan 31, 2017 at 5:11

4 Answers 4

2

The issue is in the typecasting Change List<Object[]> list = query.getResultList();

to List<Object> list = query.getResultList()

It is list of object where as your code is trying to convert and integer into List of Array.

List<Integer> list = query.getResultList() will also suffice if you are just trying to get ids

Sign up to request clarification or add additional context in comments.

7 Comments

It will not work if select statement contains several columns like in above case (id, name). And for single column cases List<Integer> should run without problem - no need to assign result list firstly to List<Object> then iterate through the whole list again in order to convert Object to Integer.
@ZaurGuliyev i have answered based on the query raised by the submiitter which says " 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;" he just needed id and wanted to know why the Code was throwing ClassCastException
there's no need to cast result you get from query.getResultList() to List<Object>, which already returns List<Integer> in selects with single expression (check general rules in my answer). The correct code is: List<Integer> list = query.getResultList();
Ok thanks Zaur Guliyev. if there are several coulmuns as in the above case , this code will work fine.
@Tom I've explained every detail in your issue, but you've accepted answer which was not complete and even wrong from professional point of view, and user has updated his answer based on provided corrections. Anyway, Good luck! :)
|
2

If you're using JPA / Hibernate for fetching single column data then it returns List<Integer> implicitly (based on type of that column), so no need to convert it to Integer list.

General rule:

  • If you are selecting an entity, then result is list of that entity typed items
  • If you are selecting primitive, then result is list of that primitive wrapper typed items
  • If you are selecting multiple expressions (id, name), then result is Object[] containing the corresponding primitives/entities

2 Comments

how can i assign object list into integer list
Just use List<Integer> not List<Object> - it should work as a general rule - updated my answer and included other rules.
0

try this one may not try in my editor but may i thing one of this solution help you.

public List<Integer> mapIds(List<Object[]>  list) {
    List<Integer> ids = new ArrayList<Integer>();
    if(list != null && !list.isEmpty()) {
        int id;
        for(Object[] object : list) {
            id = (int) (object[0] != null ?  object[0] : null);
            ids.add(id);
     }
 }
 return ids;
}

1 Comment

it gives the same exception. it works fine when the query retrieves both c.id,c.name. and fails when it retrieves c.id only
0

Although the question is lacking a lot of information, the most likely reason for the ClassCastException is that you're trying to cast objects that are not of type Integer to type Integer. I'm not sure what's in the list that you're passing into the method, but if the list does indeed contain some objects of type Integer, then what you could try is to check for that before converting element object[0] into Integer,

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 && object[0] instanceof Integer) ? (Integer) object[0] : null);
            ids.add(id);
            }
        }
    }
    return ids;
}

However, if the objects in list don't contain any Integer objects, then the only way to achieve what you're doing is to initialize an Integer object for each object[0] by somehow retrieving the int value in each. Only you know how to do this.

1 Comment

it gives the same exception.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.