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 :
Session session=HibernateUtil.getSession();
session.beginTransaction();
Query q=session.createQuery(hql);
AliasToEntityMapResultTransformer INSTANCE=new AliasToEntityMapResultTransformer();
q.setResultTransformer(INSTANCE);
List<Map<String,Object>> aliasToValueMapList=q.list();
for (Map<String,Object> map : aliasToValueMapList)
for (Map.Entry<String,Object> entry : map.entrySet()) System.out.println(entry.getKey()+" - "+entry.getValue());
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
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 ?
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 ?