2

I have this strange behavior with Hibernate SQLQuery.list() method.

Following is the description of the issue:

I have a sql select query which retrieves only single column(group) from the database (i.e., select group from peopleGroup where groupid = 10) And i'm recieving the result of the above list in List of Object array
i.e,

SQLQuery hQuery = session.createSQLQuery("select group from peopleGroup where groupid = 10");
List<Object[]> result = (List<Object[]>)hQuery.list();

Ideally, the result should contain a list of object arrays but when I inspect, 0'th index of the result contains String object instead of an Object array.
However if I use more than one column let's say 2 columns in the select clause of the query I was able to see that 0'th index of the result as Object array i.e., Object[2]={"group","groupid"};

How do I get the Object array even if I have only one column mentioned in the select clause of the query?

1
  • 2
    You wouldn't..... Commented Aug 9, 2017 at 7:30

3 Answers 3

1

Docs states:

List list()

Return the query results as a List. If the query contains multiple results per row, the results are returned in an instance of Object[].

Convert it by yourself, like so.

List<Object[]> l = new ArrayList<>();
for(Object o : query.list()) {
  Object[] arr = {o};
  l.add(arr);
}
Sign up to request clarification or add additional context in comments.

2 Comments

I understand the doc now but do i have any option of achieving my goal which is though i have single column in the select clause i need to put that returned object in object array ?
Convert it by yourself. Adding an example.
0

you can create a object mapper like below code

public class QueryMapper {
    private String group;
    //setter and getter
    }

And you have to change your code like below

SQLQuery hQuery = session.createSQLQuery("select group from peopleGroup where groupid = 10");
List<Object[]> result = (List<Object[]>)hQuery.list();

List<QueryMapper> list = new ArrayList<QueryMapper>();

        for(Object[] object: result){
            QueryMapper queryMapper = new QueryMapper();
            if(object[0]!=null){
                queryMapper.setGroup((String)object[2]);
            }
            list.add(queryMapper);
        }

Comments

0

All i wanted is to fix the ClassCastException which was caught during the assignment of result.get(0) to the container i.e., container = result.get(0);

Since the value returned by mentioned list() method contains object in case of single column in the select clause of the query and i won't be allowed to cast from certain object to Object[](Object array). Instead i have tried a work around like below

Already existing code

SQLQuery hQuery = session.createSQLQuery("select group from peopleGroup where groupid = 10");
List<Object[]> result = (List<Object[]>)hQuery.list();
Object[] container = result.get(0);

now i have put condition like below to decide how to assign value to the Object[]

SQLQuery hQuery = session.createSQLQuery("select group from peopleGroup where groupid = 10");
List<Object[]> result = (List<Object[]>)hQuery.list();
Object[] container = null;
if(result.get(0) instanceof Object[])
   container = result.get(0);
else {
     container = new Object[1];
     container[0] = result.get(0);
      }

The above solution seems working in my case !

Comments

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.