-
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
Regression for method-based query where predicate compares primary key of @ManyToOne
relationship
#3349
Comments
@ManyToOne
relationship
thanks for reporting and the reproducer - have you tried to do the same query with plain JPA/hibernate as well? |
I suppose you're referring to JPQL, right @christophstrobl? I've just added the following to the reproducers: @Query("SELECT b FROM Book b WHERE b.author.id = :authorId")
List<Book> findAllByAuthorIdWithJPQL(@Param("authorId") Long authorId); It looks good on all versions: 2.7.18select book0_.id as id1_1_, book0_.author_id as author_i3_1_, book0_.name as name2_1_ from book book0_ where book0_.author_id=1; 3.0.13select b1_0.id,b1_0.author_id,b1_0.name from book b1_0 where b1_0.author_id=1; 3.1.8select b1_0.id,b1_0.author_id,b1_0.name from book b1_0 where b1_0.author_id=1; 3.2.2select b1_0.id,b1_0.author_id,b1_0.name from book b1_0 where b1_0.author_id=1; 3.3.0-SNAPSHOTselect b1_0.id,b1_0.author_id,b1_0.name from book b1_0 where b1_0.author_id=1; |
For completeness, I also tried with private static Specification<Book> byAuthorId(Long authorId) {
return (root, query, builder) -> builder.equal(root.get("author").get("id"), authorId);
} It also looks good on all versions: 2.7.18select book0_.id as id1_1_, book0_.author_id as author_i3_1_, book0_.name as name2_1_ from book book0_ where book0_.author_id=1; 3.0.13select b1_0.id,b1_0.author_id,b1_0.name from book b1_0 where b1_0.author_id=1; 3.1.8select b1_0.id,b1_0.author_id,b1_0.name from book b1_0 where b1_0.author_id=1; 3.2.2select b1_0.id,b1_0.author_id,b1_0.name from book b1_0 where b1_0.author_id=1; 3.3.0-SNAPSHOTselect b1_0.id,b1_0.author_id,b1_0.name from book b1_0 where b1_0.author_id=1; |
CriteriaBuilder cb = entityManager.getCriteriaBuilder();
CriteriaQuery<Book> cq = cb.createQuery(Book.class);
Root<Book> root = cq.from(Book.class);
cq.select(root).where(cb.equal(root.get("author").get("id"), homer.getId()));
entityManager.createQuery(cq).getResultList(); will produce expected sql
|
@scordio It works as expected if you downgrade hibernate to |
Thanks @quaff for helping pinpoint the root cause! Closing in favor of HHH-17706 and hibernate/hibernate-orm#7782. |
I am reopening this issue as hibernate/hibernate-orm#7782 has been rejected. @mbladel mentioned in HHH-17706:
Is it something that could be done in Spring Data JPA? |
Here is workaround: interface BookRepository extends JpaRepository<Book, Long> {
default List<Book> findAllByAuthorId(Long authorId) {
Author author = new Author();
author.setId(authorId);
return findAllByAuthor(author);
}
List<Book> findAllByAuthor(Author author);
} |
Thank you, @christophstrobl! Unless someone is working on it soon, I'm also happy to take a look and come up with a proposal. |
thank you @scordio - PRs are always welcome :) |
Given the following entities:
and the following repository:
findAllByAuthorId
seems to trigger an unnecessary JOIN.I initially noticed this behavior on Spring Boot 2.7.14 with DB2 for z/OS so I tried to reproduce it locally on newer Spring Boot versions.
Surprisingly, 3.0.x and 3.1.x seem to work properly, i.e., no unnecessary JOIN, while the JOIN is back again on 3.2.x and 3.3.x. That's why I mentioned it as a regression in the title.
Here are the derived queries for each Spring Boot version with an H2 database, captured with P6Spy:
2.7.18
3.0.13
3.1.8
3.2.2
3.3.0-SNAPSHOT
Reproducers:
The text was updated successfully, but these errors were encountered: