Lukas Witzani (
https://hibernate.atlassian.net/secure/ViewProfile.jspa?accountId=557058%...
) *updated* an issue
Hibernate ORM (
https://hibernate.atlassian.net/browse/HHH?atlOrigin=eyJpIjoiNGE2NWZlMjQ1...
) / Bug (
https://hibernate.atlassian.net/browse/HHH-16053?atlOrigin=eyJpIjoiNGE2NW...
) HHH-16053 (
https://hibernate.atlassian.net/browse/HHH-16053?atlOrigin=eyJpIjoiNGE2NW...
) join fetch in @Query has no effect for for nested entities with @DiscriminatorValue
(SINGLE_TABLE inhertitance) (
https://hibernate.atlassian.net/browse/HHH-16053?atlOrigin=eyJpIjoiNGE2NW...
)
Change By: Lukas Witzani (
https://hibernate.atlassian.net/secure/ViewProfile.jspa?accountId=557058%...
)
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/l...
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.atlass...]
and maybe related to:
*
[
https://hibernate.atlassian.net/browse/HHH-15876|https://hibernate.atlass...]
*
[
https://hibernate.atlassian.net/browse/HHH-15829|https://hibernate.atlass...]
(
https://hibernate.atlassian.net/browse/HHH-16053#add-comment?atlOrigin=ey...
) Add Comment (
https://hibernate.atlassian.net/browse/HHH-16053#add-comment?atlOrigin=ey...
)
Get Jira notifications on your phone! Download the Jira Cloud app for Android (
https://play.google.com/store/apps/details?id=com.atlassian.android.jira....
) or iOS (
https://itunes.apple.com/app/apple-store/id1006972087?pt=696495&ct=Em...
) This message was sent by Atlassian Jira (v1001.0.0-SNAPSHOT#100214- sha1:771690a )