aspose file tools*
The moose likes Object Relational Mapping and the fly likes need to get column names in resultant table of sql query Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "need to get column names in resultant table of sql query" Watch "need to get column names in resultant table of sql query" New topic
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: need to get column names in resultant table of sql query