Criteria api joins

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)

We have seen how to perform inner joins and outer joins using jpql. Let us see what criteria api joins are and how we can create criteria api joins.

Criteria API Joins:

Criteria api joins can be performed with the help of Join interface. Join type acts similar to joins in jpql. The join variable in JPQL corresponds to variable of type Join in criteria. Criteria api joins can be declared using Join<X,Y> where X is the source entity and Y is target entity.

Consider we have a model as shown in the diagram

JPA Inner joins

We would like to select the phones of a teacher with first name as “prasad” using joins. A simple JPQL for that would be

Criteria api join can be created using code below.

You can notice here

  • Join<Teacher,Phone> phones = teacher.join("phones") creates a join between source entity Teacher and target entity Phone on the collection field named phones.
  • Variable phones is similar to the p variable that represents join in JPQL.
  • We haven’t specified any JoinType in join() method as argument hence by default its an inner join. You can create an outer join using teacher.join("phones",JoinType.LEFT)

We can execute above criteria api joins query as follows.

 

This prints information of phone numbers of teacher with first name “prasad”.

Number = 98474643454 Type = home
Number = 453454353 Type = office

I hope this article helped understand criteria api joins.

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.

14 thoughts on “Criteria api joins

  • November 24, 2014 at 4:19 pm
    Permalink

    Nice easy tutorial, with good examples- I’m new to Criteria and that sure helped! Thanks

    Reply
  • April 11, 2016 at 6:03 am
    Permalink

    Hi there,

    Thanks for this.

    Where are you calling the createQuery method from? In your example you specify ’em’

    Reply
    • April 11, 2016 at 8:54 am
      Permalink

      I’m not sure I’m following your question. createQuery is called on EntityManager instance only. This example assumes you have already created EntityManager instance.

      Reply
  • April 22, 2016 at 6:37 pm
    Permalink

    Hi Prasad,

    Is there a way we can get the query string when using Criteria Query. For complex queries where different conditions are used to build the query, it gets difficult to debug.

    Reply
  • April 28, 2016 at 3:01 am
    Permalink

    Thanks Prasad. This is exactly the kind of ‘end to end’ example that should be in the tutorial documentation, but isn’t. I second Bismy’s question about getting the query string when using Criteria Query. Do you know of any way to do this ?

    Reply
  • September 25, 2016 at 2:10 am
    Permalink

    Hi Prasad,

    Can we write criteria query on entities where actual physical relationship is not defined in database?

    example: If there is no foreign key constraint between teacher and phone in DB then can we still defined criteria query to join those tables?

    Reply
  • October 1, 2016 at 12:47 am
    Permalink

    Could you please share the source code ?

    Reply
    • October 1, 2016 at 10:21 am
      Permalink

      Isn’t it already shared? If you are expecting a full project in which I have written this example, I will have to search where I’ve written it 🙂 Its been long time since I published this article. I will share as soon as possible.

      Reply
  • December 14, 2016 at 1:34 pm
    Permalink

    Hi Can i ask question on this

    Reply
      • December 14, 2016 at 5:19 pm
        Permalink

        I have given the Stackover link. Please check

        Reply
  • August 9, 2017 at 12:28 am
    Permalink

    org.hibernate.PersistentObjectException: detached entity passed to persist: Employee

    Reply

Leave a Reply

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