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.Suppose if we write a query to return only selected columns, then hibernate returns a list of object array. This object array represents the columns for each row.Still not getting my point lets see with an example
Before giving the example, I am not going to tell you how to get the hibernate session and all stuff as it is of not much importance to us now.
I 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 i am 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 i call the above namedQuery it actually fetches the data in a list of object array as already dicussed.In order to acutally show the data I 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 based 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 resultTransformer will do.We have to set them as per our requirements.
Question: How to set the transformers.
Answer: see below example ...
Hibernate has a 'Transformers' class which has the following resulttransformers(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
First one as the name tells it converts the result i.e. List of Object Array with list of maps with alias as key...
going back to our example what happens if i set this transformer to our query lets see...
List results= session.getNamedQuery("getEmployeeData").setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP).list();
Now to get the first employee i 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).
So its easy to depend on this keys rather than on indexing data. But if one feels that sending a map from service to client costs(I dont have much idea on performance cost) more then we have to depend on index based data.
Want to see tutorials on more Technologies and latest updates go to http://prasad.marrapu.googlepages.com
3 comments:
very well said. Thank u for the post. Hibernate is one thing.. which has a bad documentation. so keep writing.. :)
I agree with rvi on hibernate documentation.I was looking exactly on this solution.Thank u for recording it
Have you faced the issue of Select Order mismatch with the Alias-to-Entity transformer.
Map does not preserve the order of the select columns.
Post a Comment