-
Notifications
You must be signed in to change notification settings - Fork 1.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Specifications with sort creates additional join even though the entity was already fetched #2253
Comments
The test is green. The problem seems to be that root has a "fetch" and a "join" which both end up being translated to a `JOIN` when converted to SQL. See #2253
This behaviour is actually correct. The problem with a
This talks about JPQL but I think it is save to assume the equivalent holds true for the Criteria-API used by a This means the fetch-join must not and can not be used in a WHERE-clause or ORDER-BY-clause. This of course clashes on the SQL level with the |
@schauder considering the entities described in this issue, if I create a specification
please note the equal predicate then
then creates SQL
fetch-join is used in the WHERE clause. No additional join is created. based on your statement
Are you sayin that observed behaviour is a bug and additional join should have been created? |
According to my understanding of the specification this is indeed wrong behaviour. I reopen the issue. |
I created a sample project and an issue with the JPA Spec in order to get clarification. |
Thanks for the issue. |
+1
but i want
|
Did you find any solution to this? |
I had to right my own specification for this whole thing and override my Spring JPA repository handling of the query
|
Hi, specifications with sort creates additional join even though the entity was already fetched. The result is that the table is then left joined twice. Consider setup:
entities
specifications
then
List<T> findAll(Specification<T> spec, Sort sort);
creates SQL query
B entity is joined twice. If the query is distinct, then it fails with SQL error
Expression #1 of ORDER BY clause is not in SELECT list, references column ... B1.name ... which is not in SELECT list; this is incompatible with DISTINCT
The SQL error in this case is correct, order by clause is not in select list. The sort should not create additional join if the entity was already fetched or joined.
The commit that introduced this behaviour is 43d1438
Previously, it checked whether there already is a fetched entity (isAlreadyFetched() method) if yes then it didn’t create additional join (getOrCreateJoin() method). The logic was changed to check whether the entity was already inner-joined. But even if I change the join type in specification to JoinType.INNER (root.fetch(A_.b, JoinType.INNER);) it still creates additional join.
Possible duplicate of #2206
The text was updated successfully, but these errors were encountered: