| Author |
need to get column names in resultant table of sql query
|
sudheer kiran
Ranch Hand
Joined: Jun 26, 2008
Posts: 237
|
|
hi all,
i have a sql query like "select name as firstname from employee";
in hibernate how can i find the column names of the resultant table. through query i can know the name but i don't want to parse the query.
i wan to get all the column names of the resultant data.
any suggestions?.
thanks,
|
Sudheer
SCWCD, SCJP 5
|
 |
Paul Sturrock
Bartender
Joined: Apr 14, 2004
Posts: 10336
|
|
|
Not sure I follow, you know the column name of your result set - its "firstname". Why do you need to do this?
|
JavaRanch FAQ HowToAskQuestionsOnJavaRanch
|
 |
sudheer kiran
Ranch Hand
Joined: Jun 26, 2008
Posts: 237
|
|
in case of ling statements which will contain several column names i need to parse the query and get all column names in order.
if query size increases parsing becomes complex.
i got the solution for my problem.
Query query=session.createSQLQuery("your query");
query.setResultTransformer(AliasToEntityMapResultTransformer.INSTANCE);
List<Map<String,Object>> aliasToValueMapList=query.list();
Query query=session.createSQLQuery("your query");
query.setResultTransformer(AliasToEntityMapResultTransformer.INSTANCE);
List<Map<String,Object>> aliasToValueMapList=query.list();
As you can figure out from code, the list contains Map objects representing each row. Each Map object will have column name as key and value as value.
Note: This work for SQLQuery, if your using AliasToEntityMapResultTransformer on hql query without specifying aliases you will get index value as key.
|
 |
 |
|
|
subject: need to get column names in resultant table of sql query
|
|
|