Why I have to use Object[] to print the list if I select specific columns:
Session session = DaoSF.getSessionFactory().openSession();
List<Object[]> list = new ArrayList<Object[]>(); /* Object[] */
Query criteria = session.createQuery(
"select test.col1, test.col2, test.col3
"from Test test " +
"group by test.col1, test.col2, test.col3");
list = criteria.list();
for (Object[] item : list)
{
System.out.println(item[1] + ", " + item[2] + ", " + item[3]);
}
And why is it possible to iterate the same select (select * — not the specific columns) using original Test object?
List<Test> list = new ArrayList<Test>(); /* Test */
Query criteria = session.createQuery(
"from Test test " +
"group by test.col1, test.col2, test.col3");
list = criteria.list();
for (Test item : list)
{
System.out.println(item.getCol1 + ", " + item.getCol2 + ", " + item.getCol3);
}
Is it possible to “convert” Object[] to Test object?
Try this approach; first create a constructor in your
Testclass:Now in the query, you can say:
This will give Hibernate a so-called row-mapper constructor out of which it can construct the objects of
Test. Then you will have aList<Test>fromquery.list(). This approach has a catch that you should provide different constructors beside the default one for different possible queries.