-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Open
Labels
type: enhancementA general enhancementA general enhancement
Description
Given the following entities:
@Entity
class Author {
@Id @GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String name;
// getters and setters omitted
}
@Entity
class Book {
@Id @GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String name;
@ManyToOne(fetch = FetchType.LAZY)
private Author author;
// getters and setters omitted
}
and the following repository:
interface BookRepository extends Repository<Book, Long> {
Book save(Book book);
List<Book> findAllByAuthorId(Long authorId);
}
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
select book0_.id as id1_1_, book0_.author_id as author_i3_1_, book0_.name as name2_1_ from book book0_ left outer join author author1_ on book0_.author_id=author1_.id where author1_.id=1;
3.0.13
select b1_0.id,b1_0.author_id,b1_0.name from book b1_0 where b1_0.author_id=1;
3.1.8
select b1_0.id,b1_0.author_id,b1_0.name from book b1_0 where b1_0.author_id=1;
3.2.2
select b1_0.id,b1_0.author_id,b1_0.name from book b1_0 left join author a1_0 on a1_0.id=b1_0.author_id where a1_0.id=1;
3.3.0-SNAPSHOT
select b1_0.id,b1_0.author_id,b1_0.name from book b1_0 left join author a1_0 on a1_0.id=b1_0.author_id where a1_0.id=1;
Reproducers:
- 2.7.18: BookRepositoryTest
- 3.x: BookRepositoryTest (different versions covered with workflow matrix)
Metadata
Metadata
Assignees
Labels
type: enhancementA general enhancementA general enhancement
Activity
[-]Regression for method-based query where predicate compares primary key of @ManyToOne relationship[/-][+]Regression for method-based query where predicate compares primary key of `@ManyToOne` relationship[/+]christophstrobl commentedon Feb 2, 2024
thanks for reporting and the reproducer - have you tried to do the same query with plain JPA/hibernate as well?
scordio commentedon Feb 3, 2024
I suppose you're referring to JPQL, right @christophstrobl?
I've just added the following to the reproducers:
It looks good on all versions:
2.7.18
3.0.13
3.1.8
3.2.2
3.3.0-SNAPSHOT
scordio commentedon Feb 3, 2024
For completeness, I also tried with
JpaSpecificationExecutor
and aSpecification
:It also looks good on all versions:
2.7.18
3.0.13
3.1.8
3.2.2
3.3.0-SNAPSHOT
quaff commentedon Feb 4, 2024
will produce expected sql
quaff commentedon Feb 4, 2024
@scordio It works as expected if you downgrade hibernate to
6.2.20.Final
which is used by3.1.8
, I confirm it is a regression introduced by hibernate 6.4.1.Final, I reported it to hibernate team, see https://hibernate.atlassian.net/browse/HHH-17706.scordio commentedon Feb 4, 2024
Thanks @quaff for helping pinpoint the root cause!
Closing in favor of HHH-17706 and hibernate/hibernate-orm#7782.
scordio commentedon Feb 5, 2024
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?
quaff commentedon Feb 6, 2024
Here is workaround:
christophstrobl commentedon Feb 6, 2024
Thank you both @scordio & @quaff - we'll see if there's anything we can do on our side.
25 remaining items