title: How to get column names from Hibernate query result in Java?
body: <p>I'm writing a Java app that uses Hibernate to get data, in my app I have an input text area that takes an user typed-in sql command string and run that through Hibernate to get whatever data the user queries, so I don't know beforehand what the result table might look like and therefore don't know the column names, but I do need to display user query result in a table with column names relate to the data fields, how to achieve that in Hibernate ? I've tried the following code :</p>
  
  <pre class="lang-java prettyprint-override"><code>  Session session=HibernateUtil.getSession();
    session.beginTransaction();
    Query q=session.createQuery(hql);
  
    AliasToEntityMapResultTransformer INSTANCE=new AliasToEntityMapResultTransformer();
    q.setResultTransformer(INSTANCE);
    List&lt;Map&lt;String,Object&gt;&gt; aliasToValueMapList=q.list();
  
    for (Map&lt;String,Object&gt; map : aliasToValueMapList)
      for (Map.Entry&lt;String,Object&gt; entry : map.entrySet()) System.out.println(entry.getKey()+" - "+entry.getValue());
  </code></pre>
  
  <p>It gave me the following error message : Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: sakila.entity.Actor cannot be cast to java.util.Map</p>
  
  <p>It's pointing to the 1st for loop, since I'm new to Hibernate, don't know if it's doable in it, how to fix the above code ? Could someone show me some sample code that works in my case ?</p>
  
  <p>Edit :
  As Marcel Stör mentioned below, I need to be able to allow both situations to happen and not limit users' ability to query any data, what's the best way to do it ?</p>