Criteria api path expressions

The following two tabs change content below.
Prasad Kharkar is a java enthusiast and always keen to explore and learn java technologies. He is SCJP,OCPWCD, OCEJPAD and aspires to be java architect.

Latest posts by Prasad Kharkar (see all)

Previously, we’ve seen about the query roots in criteria api. This article deals with the use of query roots in criteria api path expressions.

Criteria API path expressions:

Just like JPQL, path expressions are very important aspect of criteria api as they enable us to access the properties of entities represented by query root objects.

Consider following JPQL which returns teacher entities with first name as ‘Prasad’.

Here, we know t.firstName is the path expression; plain and simple, but in case of criteria api path expressions, java methods need to be invoked on query roots. first name of teacher can be accessed as follows.

teacher object is the Root<Teacher>. The get() method acts like the dot operator in JPQL. It is defined in Path interface extended by Root interface.

A complete example demonstrating criteria api path expressions is as below.

This will print the result.

Prasad Kharkar 20000

I hope this example helped understand criteria api path expressions in jpa.

 

References for further study :

Share Button

Prasad Kharkar

Prasad Kharkar is a java enthusiast and always keen to explore and learn java technologies. He is SCJP,OCPWCD, OCEJPAD and aspires to be java architect.

Leave a Reply

Your email address will not be published. Required fields are marked *