2

I am using hibernate's createSQLQuery method to return a list.

code:

Query query=session.createSQLQuery("select name,marks,grade from student where id=12");
List list = query.list();

Also I want to know what will be returned in list. Will it contain just values or will it also contain column name.

If it also contain column name, then I want to break every element of this list to col name and its value; so that i can put Map<String,String> (Map<colName,colValue>) to a new list List<Map<String,String>> How can I do this.

1 Answer 1

2

createSQLQuery returns

List<Object[]>

, it will contain just values. You can extract it as you like, for example

    List<Map<String,String>> = new ArrayList<HashMap<String,String>>();  
    for (Object row: queryResult)
                    {
                        Map <String, String> map = new HashMap<String, String>();
                        Object[] versionId = (Object [])row;
                        map.put("name", versionId[0]);
                        ... 
                        result.add(map);
                    }

Or you can create a model and transer query Result to list of entities look here spring hibernate.createSQLQuery return as custom entity

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

1 Comment

Also, to get a List<Map<columnName, columnValue>> you can use createSQLQuery(...).setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP)

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.