Skip to content
Advertisement

JPA Criteria Builder query with inner SELECT statement

I need to create the below SQL join condition using JPA criteria builder,

Below is the entity class and JPA query builder.

I am able to build the query with join condition but cannot create the inner SELECT query. How can i create a inner select query for join clause?

Requirement description: The below is the input table and output required.

Student Table

Output

Advertisement

Answer

I have not tried this and it cloud fail miserably with an exception. But Can you give a try?

  • Remove the studentSelf and studentRef if you added them just for this query. @ManyToOneonstudentSelf` is not either as it will point to many records

  • I think the following query is equivalent to what you are trying to achieve.

  • Then I am trying to translate it to CriteriaQuery

Update

Updated subquery.correlate(mainStudent); to subQuery.from(Student.class); based on vinay-s-g comments

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement