|
List Result Transformer
The following example shows how to use list result transformer.
Refer first example for the configuration and mapping.
|
package com.bethecoder.tutorials.hibernate.basic.tests;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.transform.Transformers;
import com.bethecoder.tutorials.hibernate.basic.util.HibernateUtil;
public class HQLTransformListTest {
/**
* @param args
*/
public static void main(String[] args) {
Session session = HibernateUtil.getSessionFactory().openSession();
session.beginTransaction();
String HQL_QUERY = "select comp.id, comp.name from Company comp";
Query query = session.createQuery(HQL_QUERY);
query.setResultTransformer(Transformers.TO_LIST);
List<?> rows = query.list();
System.out.println("Selected row count : " + rows.size());
for (Object row : rows) {
System.out.println(row); //Each row is a list of properties in the query
}
session.getTransaction().commit();
session.close();
}
}
|
| |
It gives the following output,
Selected row count : 10
[1, PQR]
[2, ABC]
[3, MNO]
[4, IJK]
[5, ART]
[6, RNK]
[7, AOQ]
[8, BQO]
[9, CFR]
[10, CFR]
|
|