Book Image

Java Hibernate Cookbook

By : Yogesh Prajapati, Vishal Ranapariya
Book Image

Java Hibernate Cookbook

By: Yogesh Prajapati, Vishal Ranapariya

Overview of this book

This book will provide a useful hands-on guide to Hibernate to accomplish the development of a real-time Hibernate application. We will start with the basics of Hibernate, which include setting up Hibernate – the pre-requisites and multiple ways of configuring Hibernate using Java. We will then dive deep into the fundamentals of Hibernate such as SessionFactory, session, criteria, working with objects and criteria. This will help a developer have a better understanding of how Hibernate works and what needs to be done to run a Hibernate application. Moving on, we will learn how to work with annotations, associations and collections. In the final chapters, we will see explore querying, advanced Hibernate concepts and integration with other frameworks.
Table of Contents (15 chapters)
Java Hibernate Cookbook
About the Authors
About the Reviewers

Using basic projection

First of all, let's understand what projection is. It is a class provided by hibernate that is used to select a particular field while querying. Apart from that, we can use some built-in aggregation functions provided by hibernate.

Here, we will only consider a basic projection. Now, the scenario is that we need only the id and firstName fields from the employee table to set them in projection.

If we have only one field to select, we can directly use the following code:

setProjection(Projections object);

However, if we need more than one column in the result, we need to use the ProjectionList class, as follows:

setProjection(ProjectionList object);

When we use ProjectionList, hibernate returns List<Object> in the result. So, it's better to use ALIAS_TO_ENTITY_MAP for the ease of access of fields; however, it depends on the actual requirement.

How to do it...

Here, we will take a look at two scenarios that show how to select single and multiple fields while querying with...