h1. Summary
* after updating spring boot to 3.0.1 (which also updates hibernate to 6.1.6.Final) join fetching of nested entities does not work anymore when the nestedEntity reference is in an abstract superclass ("BodyPart") that uses @Inheritance(strategy = InheritanceType.SINGLE_TABLE) * the result is a LazyInitializationException when accessing fields in the nested entity although join fetch has been specified in the @Query
h2. Requisites
* entity ("Person") exists that has a field which is a set of other entities ("Set<Leg>") * these other entities are derived from an abstract super class ("Leg extends BodyPart") * the abstract super class' inheritance strategy is SINGLE_TABLE * the abstract super class has a nested entity ("NestedEntity") which has some fields
!diagram.png|width=614,height=479!
h2. Reproduction
* [https://github.com/lwitzani/hibernate6JoinFetchIssue|https://github.com/lwitzani/hibernate6JoinFetchIssue|smart-link] there are two branches in this repository (one with hibernate 5 and on with hibernate 6) * execute the test PersonPersistenceTest.shouldJoinFetchTheNestedEntitiesOfLegs * the test basically ** creates and saves a Person with one Leg and that Leg has a NestedEntity ** fetches the Person by findByIdJoinFetchLegsWithNestedEntity (a @Query method in the Repository). The query uses join fetch to specify that person->legs is fetched and also that legs->nestedEntity is fetched. ** tries to access the nested entity via person->legs->nestedEntity->name
h2. Observations in Hibernate 5.6.14.Final
* the query created by findByIdJoinFetchLegsWithNestedEntity is working correct * it join fetches both the body_part and the nested_entity table * it uses a left OUTER join for that * it only defines the clause legs1_.discriminator='LegBodyPart' once
!hibernate5_working.png|width=1501,height=926!
h2. Observations in Hibernate 6.1.6.Final
* the query created by findByIdJoinFetchLegsWithNestedEntity is NOT working correct ** there is no second left join which would join the nested_entity table ** it does NOT use a left join (not outer) ** also, it is strange that there is a duplicate join clause l1_0.discriminator='LegBodyPart' and l1_0.discriminator='LegBodyPart' * result is a LazyInitializationException when accessing the name
!hibernate6_failing.png|width=1480,height=926!
This issue might be heavily related to [https://hibernate.atlassian.net/browse/HHH-16037|https://hibernate.atlassian.net/browse/HHH-16037|smart-link]
and maybe related to:
* [https://hibernate.atlassian.net/browse/HHH-15876|https://hibernate.atlassian.net/browse/HHH-15876|smart-link] * [https://hibernate.atlassian.net/browse/HHH-15829|https://hibernate.atlassian.net/browse/HHH-15829|smart-link] |
|