Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When to use Hibernate projections?

I am a little confused about Hibernate's projections and criteria. When to use projections and when to use criteria?

like image 632
reddy Avatar asked Sep 21 '11 10:09

reddy


People also ask

What is DTO projection in Hibernate?

DTO is an abbreviation that stands for Data Transfer Object. Originally, Martin Fowler defined a DTO in his famous book Patterns of Enterprise Application Architecture as: An object that carries data between processes in order to reduce the number of method calls.

What are JPA projections?

Projection is one of the first things you're probably thinking about when implementing a query with Spring Data JPA. This is because projection defines the entity attributes and the database columns returned by your query. So, selecting the right columns is important for your business logic.

What are projections in Java?

Java Projections are pure Java classes that serve as proxies for Caché classes within a Java application. They provide the communication link between Java client objects and the Caché Objects on the Caché data server. Typically, a Java application contains a Java projection for each Caché class in the database.


2 Answers

They're not mutually exclusive, you can use both at the same time. Projections are generally used in the context of some Criteria.

To put it simple, Hibernate Projections are used in order to query only a subset of the attributes of an entity or group of entities you're querying with Criteria. You can also use Projections to specify distinct clauses and aggregate functions like max, sum and so on. It's like referring to which data you're fetching. Like modifying the select clause in an SQL query.

Hibernate Criteria are used to define conditions the data has to satisfy in order to be selected. It's like referring to how is the data you're fetching. Like modifiying the from and where clauses of an SQL query.

Note that this how and which is not strictly true, it's just an orientation aimed to aid the OP. You can change which data you're fetching with createCriteria(String associationPath) for instance.

I'd suggest to take a look at this article Hibernate: Criteria Queries in Depth

like image 105
Xavi López Avatar answered Oct 02 '22 13:10

Xavi López


Projections are used to execute aggregate operations and to get single column query,with Restrictions we can access a ROW but with PROJECTIONS we can access whole COLUMN

EX -

public static void main(String[] args) {     SessionFactory factory = new Configuration().configure().addAnnotatedClass(Student.class).buildSessionFactory();     Session session = factory.getCurrentSession();     try {         session.beginTransaction();         Criteria c = session.createCriteria(Student.class);         Projection p = Projections.property("lastName");         List<String> students = c.setProjection(p).list();         for(String s:students)             System.out.println(s);         session.getTransaction().commit();         session.close();     } finally {         factory.close();     } } 

In the above example i used projection call to ADD a projection property "name" to the criteria. It returns winchester winchester winchester winchester , which is the lastName COLUMN in the table.

Output =

Hibernate: select this_.last_name as y0_ from student this_ winchester winchester winchester winchester

Note - We can add only one projection, if we add more than 1 projection previous one will be overridden. if you want to add more than one projection you will nedd ProjectionList class

Orignal Table -

enter image description here

like image 29
Rishabh Jain Avatar answered Oct 02 '22 14:10

Rishabh Jain