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.