Hibernate querying unmapped tables
hibernate, object
Solution
You could also use constructor expressions, like this:
List<MyClass> dtos = session.createQuery("SELECT NEW com.example.MyClass( e.name, e.data) FROM Entity e").list();
The downside is that you would also have to create the `Entity`, mapping the table in order to query for it. You can do it with annotations, so you won't have to create a `hbm` file.
Technically, it is not quite what you asked for. I have found it useful for mapping report-like queries though, so perhaps it is what you are looking for.
Problem
Is there a way to use java bean functionality on unmapped tables? So, I have a table that is used only for reads, it will never be modified. I need to query it to only to display data. But I don't want the Object [] return type that hibernate defaults to when querying unmapped objects. I want to retrieve the results into a custom typed collection. But I will have to create the hbm file to do this. Is there anyway to just create the custom type and no hbm file? Businessobj method that loads the results: ``` loadResults() { String qry = "select col1, col2 from table"; List<CustomType> result = (ArrayList<CustomType>) dao.HQLWithTransformer(qry, new CustomTransformer()); } ``` Custom transformer: ``` public class CustomTransformer implements ResultTransformer { @Override public Object transformTuple(Object[] rowdata, String[] arg1) { return new CustomType(String.valueOf(rowdata[0]),String.valueOf(rowdata[1])); return null; } @Override public List transformList(List arg0) { return null; } ``` } DAO method: ``` public Collection HQLWithTransformer(String qry, ResultTransformer rt){ List<?> al=null; try { Query q = sess.createQuery(qry); q.setResultTransformer(rt); al = (ArrayList<?>)q.list(); } catch(HibernateException he) { log.debug("Hibernate Exception", he); } finally { sess.close(); } return al; } ```