Search icon
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
Java Hibernate Cookbook

You're reading from  Java Hibernate Cookbook

Product type Book
Published in Sep 2015
Publisher
ISBN-13 9781784391904
Pages 250 pages
Edition 1st Edition
Languages

Table of Contents (15) Chapters

Java Hibernate Cookbook
Credits
About the Authors
About the Reviewers
www.PacktPub.com
Preface
1. Setting Up Hibernate 2. Understanding the Fundamentals 3. Basic Annotations 4. Working with Collections 5. Working with Associations 6. Querying 7. Advanced Concepts 8. Integration with Other Frameworks Index

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 List.

Scenario 2: Converting a result to Map

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.

Code

For example, the following code shows...

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime}