29

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

Exception in thread "main" java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to id.co.bni.switcherservice.model.SwitcherServiceSource
at id.co.bni.switcherservice.controller.SwitcherServiceController.LoadData(SwitcherServiceController.java:48)
at id.co.bni.switcherservice.controller.SwitcherServiceController.main(SwitcherServiceController.java:62)

this is my code

    Query LoadSource = session_source.createQuery("select CLIENT,SERVICE,SERVICE_TYPE,PROVIDER_CODE,COUNT(*) FROM SwitcherServiceSource" +
            " where TIMESTAMP between :awal and :akhir" +
            " and PROVIDER_CODE is not null group by CLIENT,SERVICE,SERVICE_TYPE,PROVIDER_CODE order by CLIENT,SERVICE,SERVICE_TYPE,PROVIDER_CODE");
    LoadSource.setParameter("awal", fromDate);
    LoadSource.setParameter("akhir", toDate);

    List<SwitcherServiceSource> result_source = (List<SwitcherServiceSource>) LoadSource.list();
    for(SwitcherServiceSource tes : result_source){
        System.out.println(tes.getSERVICE());
    }

any help will be pleasure :)

@raffian, did you mean like this??

List<Switcher> result = (List<Switcher>) LoadSource.list();
for(Switcher tes : result){
    System.out.println(tes.getSERVICE());
}
splatter_fadli
  • 663
  • 3
  • 15
  • 30
  • @raffian That is a hibernate API method. – Adarsh Dec 10 '13 at 06:18
  • Does this problem happens for every select query ? When some fields are retrieved from query out of all i absorb to follow the solution provided by Anikit Kulkarni. When whole fields are retrieved from query this wont be a problem splatter_fadli – Ajay Takur Jun 10 '16 at 15:55
  • the answer Gleb S provided below is better than the accepted one, imo https://stackoverflow.com/questions/20486641/ljava-lang-object-cannot-be-cast-to#answer-40000155 – Andrei Epure is hiring Aug 16 '17 at 12:07

5 Answers5

47
java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to id.co.bni.switcherservice.model.SwitcherServiceSource

Problem is

(List<SwitcherServiceSource>) LoadSource.list();

This will return a List of Object arrays (Object[]) with scalar values for each column in the SwitcherServiceSource table. Hibernate will use ResultSetMetadata to deduce the actual order and types of the returned scalar values.

Solution

List<Object> result = (List<Object>) LoadSource.list(); 
Iterator itr = result.iterator();
while(itr.hasNext()){
   Object[] obj = (Object[]) itr.next();
   //now you have one array of Object for each row
   String client = String.valueOf(obj[0]); // don't know the type of column CLIENT assuming String 
   Integer service = Integer.parseInt(String.valueOf(obj[1])); //SERVICE assumed as int
   //same way for all obj[2], obj[3], obj[4]
}

Related link

Ajay Takur
  • 5,287
  • 4
  • 31
  • 50
Aniket Kulkarni
  • 12,142
  • 9
  • 65
  • 83
8

I've faced such an issue and dig tones of material. So, to avoid ugly iteration you can simply tune your hql:

You need to frame your query like this

select entity from Entity as entity where ...

Also check such case, it perfectly works for me:

public List<User> findByRole(String role) {

    Query query = sessionFactory.getCurrentSession().createQuery("select user from User user join user.userRoles where role_name=:role_name");
    query.setString("role_name", role);
    @SuppressWarnings("unchecked")
    List<User> users = (List<User>) query.list();
    return users;
}

So here we are extracting object from query, not a bunch of fields. Also it's looks much more pretty.

Gleb S
  • 353
  • 4
  • 11
4

You need to add query.addEntity(SwitcherServiceSource.class) before calling the .list() on query.

Mahaveer Jangir
  • 451
  • 5
  • 13
2

Your query execution will return list of Object[].

List result_source = LoadSource.list();
for(Object[] objA : result_source) {
    // read it all
}
vels4j
  • 10,808
  • 4
  • 35
  • 54
  • I've try it and I got this error `cannot convert from element type Object to Object[]` – splatter_fadli Dec 10 '13 at 06:28
  • debug and find what is the element in list or you can do sout. – vels4j Dec 10 '13 at 06:30
  • I think the problem is from this code `for(SwitcherServiceSource tes : result_source)`, I try to use "for each" to get value from database but failed... I think the element in list is Array ~ – splatter_fadli Dec 10 '13 at 06:40
0

In case entire entity is being return, better solution in spring JPA is use @Query(value = "from entity where Id in :ids")

This return entity type rather than object type

babu
  • 1