Gopal Singh
3 min readOct 10, 2021

Hibernate- Result Transformer: Best Practices and Guidelines

Introduction:

Result transformers as the name suggest they are used to transform the results returned by our hibernate query to a specific format or I would say to a bean or a map.

Normally, when you query for data in hibernate it returns a domain object in that case we may not find much use of Result Transformers. But if we write a named query to return only selected columns, then hibernate returns a list of object array. This object array represents the columns for each row. Let’s see with an example

We have a pojo “Employee” with properties like id,name..etc and it is mapped to a table. Now I have a requirement to fetch the employee name and Salary for all employees. For this we are going to write a Named query in my Employee.hbm.xml

<sql-query name=”getEmployeeData”>

<return-scalar type=”java.lang.String” column=”empname “>

<return-scalar type=”java.math.BigDecimal” column=”salary”>

SELECT empname as name

. , salary as salary

. FROM Employee

</sql-query>

So when we call the above namedQuery it actually fetches the data in a list of object array as already discussed. In order to acutally show the data we have to do following things

List results= session.getNamedQuery(“getEmployeeData”).list();

To get the first Employee data

Object[] data =results.get(0);

String ename = (Long)data[0];

BigDecimal sal = (BigDecimal)data[1];

As the example shows we have to base on indexing data. Think how it would be if we can get the data based on our actual aliases we provided in the named query. This is what a Hibernate Result Transformer will do.

Types of Result Transformers

Hibernate has a ‘Transformers’ class which has the following Result Transformers (To know in deep please refer the actual implementation of this transformers)

1.Transformers.ALIAS_TO_ENTITY_MAP

2.Transformers.TO_LIST

3.Transformers.aliasToBean

Uses of Result Transformers:

  1. Transformers.ALIAS_TO_ENTITY_MAP
  2. As the name tells it converts the result i.e. List of Object Array with list of maps with alias as key.
  3. Going back to our example what happens if we set this transformer to our query let’s see…

List results=.

session.getNamedQuery (“getEmployeeData”)

.setResultTransformer (Transformers.ALIAS_TO_ENTITY_MAP).list ();

Now to get the first employee we have to do something like this

Map data =(Map)results.get(0);

String ename = (Long)data.get(“empname”);

BigDecimal sal = (BigDecimal)data.get(“salary”);

These aliases are given by us in the named query(Please refer the named query given earlier in this example).

2. Transformers. TO_LIST

As the name tells it converts the result in List that named query returns by default. We can set this transformer like,

List results=.

session.getNamedQuery (“getEmployeeData”)

.setResultTransformer (Transformers. TO_LIST).list ();

3. Transformers. aliasToBean

It converts the result in List. aliasToBean Transformer allows you to retrieve specific information in non entity beans.

This hibernate API call will allow you to run sql against your database and populate a list of pojo that are not a hibernate entity. This technique is great when you need specific information or perhaps you want information from multiple tables.

We can set this transformer like,

try {

Session session = HibernatePlugin.getSession();

. Query query;

.

query = session.getNamedQuery(“getEmployeeData “)

.setResultTransformer(Transformers.aliasToBean(Employee.class);

/* we pass bean class in aliasToBean() method to reflect.

. Properties

*/

ArrayList<Material> marketLists =

(ArrayList<Material>)query.list();

} catch (Exception e) {

log.error(“Error occurred: “ + e);

}

There are times we have a class, we would like to fill with data according the data returned from a query. The class is a simple POJO and not a Hibernate entity, So Hibernate won’t recognize this class.

This can be done in Hibernate by using Transformers. Let’s have a look on a simple example, showing how Transformers can be used. First, let’s have a look at a simple POJO class named: “XYZ”.

This class contains some statistical information. We would like to fill the statistical information of an instance, directly from running a Hibernate HQL

Best Practices and Useful Tips:

Result Transformer should be used with Named OR Scalar Query. It should not be use with native Query. If we use result Transformer with Query instead of Named Query, it will return null value.

We use aliasToBean transformer, when we need to access values from tables that are not hibernate entities, in an application that uses hibernate to access the database, we want to access a couple columns from any number of tables in your database without bringing back all the associated objects.

We use ALIAS_TO_ENTITY_MAP transformer, when we can get the data based on our actual aliases we provided in the named query instead of index based.

We use TO_LIST transformer, when we can need the data in list.

Conclusion:

Result Transformer helps to transform query’s result that is in domain object to List, MAP or Beans. To get more information, Please follow the Hibernate API