Skip to content
Advertisement

Unwanted Cross Join in JPA CriteriaQuery select in subquery

I’m getting what I think are needless CROSS JOINs when I’m doing a select IN SUBQUERY, which is hurting performance. I’m using Postgres if that makes a difference.

I’m aiming to generate the following query

But I get

Code

In reality I’m generating the queries from a user driven query builder, this code recreates the exact problem I’m having.

When using the query builder the user could end up with multiple select in subqueries so I need this to perform as well as possible.

I don’t see why I should need any join/cross join for my query to work.

Entities

Advertisement

Answer

This expression: bookRoot.get(Book_.author) means you’re joining Author to Book implicitly.

To get rid of the extra join, you would have to either use a native query, or map Book.author_id once more as a simple column:

And use Book_.authorId instead.

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