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

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.