Transforming a result
As a developer, I love this feature, as it helps the developers to transform the returned rows to List
, Map
, or user-defined Bean.
How to do it...
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
.
Scenario 1: Converting a result to List
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
Get Java Hibernate Cookbook now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.