As a developer, I love this feature, as it helps the developers to transform the returned rows to List
, Map
, or user-defined Bean.
Now we will take a look at three scenarios of the demonstration code that will convert the records returned by hibernate to List
, Map
, and Bean.
Here, we use the Transformers
class to provide the transforming mechanism to criteria
.
All the demos up to this point show that if we use the criteria.list()
method, the resultant data is always returned in List
. However, you can still use Transformers.TO_LIST
in criteria, as follows:
criteria.setResultTransformer(Transformers.TO_LIST);
This means that every row in the result will be represented as a List
.
Now, let's see how to convert the resultant data in Map
:
criteria.setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP);
This means every object from List
represents Map
.
For example, the following code shows how to transform the resultant data into Map
:
Criteria criteria = session.createCriteria(Employee.class); criteria.setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP); List list = criteria.list(); System.out.println("List: " + list); Map map = (Map) list.get(0); Employee employeeMap = (Employee) map.get(Criteria.ROOT_ALIAS); System.out.println(employeeMap.toString());
The output will be as follows:
List: [{this=Employee@3235025a}, {this=Employee@4e84c320}, {this=Employee@2644f3a2}, {this=Employee@7c7d8dfe}] Employee id: 2 first name: aarush salary: 35000.0 department: developement
When we print out a list object in the console, we can see that it's represented as key and value format in the Map
structure. We will try to get the value from map
at the zero position using the following code:
Map map = (Map) list.get(0);
The key of Map
is this
, and the value is the object of the Employee
class.
So, the standard way to access a map's value is by its key. Here, the key is this
, which is the equivalent of the Root
alias of criteria
in hibernate. So, you can access an object of Employee
with the help of the following code:
Employee employeeMap = (Employee) map.get(Criteria.ROOT_ALIAS);
This feature is useful when we select the columns in the resultant data and want to form that data in an already defined Bean
.
For example, if we select the empId
, empFirstName
, empSalary
, and the empDeptName
, we can easily form this data into an EmployeeDetail
bean, as we already have a bean defined with the name EmployeeDetail.java
with these four fields:
public class EmployeeDetail { private long empId; private String empFirstName; private double empSalary; private String empDeptName; public long getEmpId() { return empId; } public void setEmpId(long empId) { this.empId = empId; } public String getEmpFirstName() { return empFirstName; } public void setEmpFirstName(String empFirstName) { this.empFirstName = empFirstName; } public double getEmpSalary() { return empSalary; } public void setEmpSalary(double empSalary) { this.empSalary = empSalary; } public String getEmpDeptName() { return empDeptName; } public void setEmpDeptName(String empDeptName) { this.empDeptName = empDeptName; } @Override public String toString() { return " EmployeeDetail " + " Employee id: " + this.empId + " Employee FirstName: " + this.empFirstName + " Employee Salary: " + this.empSalary + " Employee DepartmentName : " + this.empDeptName; } }
Now, the following code shows how to convert the resultant data into an EmployeeDetail
bean:
Criteria criteria = session.createCriteria(Employee.class); criteria.createAlias("department", "_department"); ProjectionList projectionList = Projections.projectionList(); projectionList.add(Projections.alias(Projections.property("id"), "empId")); projectionList.add(Projections.alias(Projections.property("firstName"), "empFirstName")); projectionList.add(Projections.alias(Projections.property("salary"), "empSalary")); projectionList.add(Projections.alias(Projections.property("_department.deptName"), "empDeptName")); criteria.setProjection(projectionList); criteria.setResultTransformer(Transformers.aliasToBean(EmployeeDetail.class)); List<EmployeeDetail> employeeDetails = criteria.list(); EmployeeDetail employeeDetail = employeeDetails.get(0); System.out.println(employeeDetail.toString());
The output will be as follows:
EmployeeDetail Employee id: 1 Employee FirstName: yogesh Employee Salary: 50000.0 Employee DepartmentName : developement
To use this feature in hibernate, we need to match a resultant column alias with a fieldname in the bean. For example, here we gave empId
as an alias of the id
field.
Actually, here you will notice a new term known as projection, which we will discuss in the next section.