-
-
Notifications
You must be signed in to change notification settings - Fork 3.7k
HHH-19857 When one of the lazy attributes is null the other ones may not get initialized correctly / HHH-19856 @LazyGroup is not always respected
#11110
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
Closed
marko-bekhta
wants to merge
3
commits into
hibernate:main
from
marko-bekhta:bug/HHH-19856-lazy-problems
+194
−1
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
191 changes: 191 additions & 0 deletions
191
...core/src/test/java/org/hibernate/orm/test/mapping/lazytoone/LazyOneToOneWithCastTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,191 @@ | ||
| /* | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| * Copyright Red Hat Inc. and Hibernate Authors | ||
| */ | ||
| package org.hibernate.orm.test.mapping.lazytoone; | ||
|
|
||
| import jakarta.persistence.Entity; | ||
| import jakarta.persistence.FetchType; | ||
| import jakarta.persistence.Id; | ||
| import jakarta.persistence.JoinColumn; | ||
| import jakarta.persistence.OneToOne; | ||
| import org.hibernate.Hibernate; | ||
| import org.hibernate.annotations.LazyGroup; | ||
| import org.hibernate.testing.bytecode.enhancement.EnhancementOptions; | ||
| import org.hibernate.testing.bytecode.enhancement.extension.BytecodeEnhanced; | ||
| import org.hibernate.testing.orm.junit.DomainModel; | ||
| import org.hibernate.testing.orm.junit.SessionFactory; | ||
| import org.hibernate.testing.orm.junit.SessionFactoryScope; | ||
| import org.junit.jupiter.api.AfterEach; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| import static org.assertj.core.api.Assertions.assertThat; | ||
|
|
||
| @DomainModel( | ||
| annotatedClasses = { | ||
| LazyOneToOneWithCastTest.ContainingEntity.class, | ||
| LazyOneToOneWithCastTest.ContainedEntity.class, | ||
| } | ||
| ) | ||
| @SessionFactory | ||
| @BytecodeEnhanced | ||
| @EnhancementOptions(lazyLoading = true) | ||
| class LazyOneToOneWithCastTest { | ||
|
|
||
| @Test | ||
| void oneNullOneNotNull(SessionFactoryScope scope) { | ||
| scope.inTransaction( session -> { | ||
| ContainingEntity containingEntity1 = new ContainingEntity(); | ||
| containingEntity1.setId( 2 ); | ||
|
|
||
| ContainedEntity contained1 = new ContainedEntity(); | ||
| contained1.setId( 4 ); | ||
| contained1.setContainingAsIndexedEmbeddedWithCast( containingEntity1 ); | ||
| containingEntity1.setContainedIndexedEmbeddedWithCast( contained1 ); | ||
|
|
||
| session.persist( contained1 ); | ||
| session.persist( containingEntity1 ); | ||
|
|
||
| } ); | ||
|
|
||
| scope.inTransaction( session -> { | ||
| ContainedEntity contained = session.find( ContainedEntity.class, 4 ); | ||
|
|
||
| ContainingEntity containingAsIndexedEmbedded = contained.getContainingAsIndexedEmbedded(); | ||
| assertThat( containingAsIndexedEmbedded ).isNull(); | ||
| assertThat( Hibernate.isPropertyInitialized( contained, "containingAsIndexedEmbedded" ) ).isTrue(); | ||
| assertThat( Hibernate.isPropertyInitialized( contained, "containingAsIndexedEmbeddedWithCast" ) ).isFalse(); | ||
|
|
||
| Object containingAsIndexedEmbeddedWithCast = contained.getContainingAsIndexedEmbeddedWithCast(); | ||
| assertThat( Hibernate.isPropertyInitialized( contained, "containingAsIndexedEmbeddedWithCast" ) ).isTrue(); | ||
| assertThat( containingAsIndexedEmbeddedWithCast ).isNotNull(); | ||
| } ); | ||
|
|
||
| scope.inTransaction( session -> { | ||
| ContainedEntity contained = session.find( ContainedEntity.class, 4 ); | ||
| assertThat( contained.getContainingAsIndexedEmbeddedWithCast() ).isNotNull(); | ||
| } ); | ||
| } | ||
|
|
||
| @Test | ||
| void bothNotNull(SessionFactoryScope scope) { | ||
| scope.inTransaction( session -> { | ||
| ContainingEntity containingEntity1 = new ContainingEntity(); | ||
| containingEntity1.setId( 1 ); | ||
|
|
||
| ContainedEntity contained1 = new ContainedEntity(); | ||
| contained1.setId( 4 ); | ||
| contained1.setContainingAsIndexedEmbeddedWithCast( containingEntity1 ); | ||
| containingEntity1.setContainedIndexedEmbeddedWithCast( contained1 ); | ||
|
|
||
| contained1.setContainingAsIndexedEmbedded( containingEntity1 ); | ||
| containingEntity1.setContainedIndexedEmbedded( contained1 ); | ||
|
|
||
|
|
||
| session.persist( contained1 ); | ||
| session.persist( containingEntity1 ); | ||
|
|
||
| } ); | ||
|
|
||
| scope.inTransaction( session -> { | ||
| ContainedEntity contained = session.find( ContainedEntity.class, 4 ); | ||
|
|
||
| ContainingEntity containingAsIndexedEmbedded = contained.getContainingAsIndexedEmbedded(); | ||
| assertThat( containingAsIndexedEmbedded ).isNotNull(); | ||
| assertThat( Hibernate.isPropertyInitialized( contained, "containingAsIndexedEmbedded" ) ).isTrue(); | ||
| assertThat( Hibernate.isPropertyInitialized( contained, "containingAsIndexedEmbeddedWithCast" ) ).isFalse(); | ||
|
|
||
| Object containingAsIndexedEmbeddedWithCast = contained.getContainingAsIndexedEmbeddedWithCast(); | ||
| assertThat( Hibernate.isPropertyInitialized( contained, "containingAsIndexedEmbeddedWithCast" ) ).isTrue(); | ||
| assertThat( containingAsIndexedEmbeddedWithCast ).isNotNull(); | ||
| } ); | ||
|
|
||
| scope.inTransaction( session -> { | ||
| ContainedEntity contained = session.find( ContainedEntity.class, 4 ); | ||
| assertThat( contained.getContainingAsIndexedEmbeddedWithCast() ).isNotNull(); | ||
| } ); | ||
| } | ||
|
|
||
| @AfterEach | ||
| void tearDown(SessionFactoryScope scope) { | ||
| scope.getSessionFactory().getSchemaManager().truncateMappedObjects(); | ||
| } | ||
|
|
||
| @Entity(name = "containing") | ||
| public static class ContainingEntity { | ||
|
|
||
| @Id | ||
| private Integer id; | ||
|
|
||
| @OneToOne | ||
| private ContainedEntity containedIndexedEmbedded; | ||
|
|
||
| @OneToOne(targetEntity = ContainedEntity.class) | ||
| @JoinColumn(name = "CIndexedEmbeddedCast") | ||
| private ContainedEntity containedIndexedEmbeddedWithCast; | ||
|
|
||
|
|
||
| public Integer getId() { | ||
| return id; | ||
| } | ||
|
|
||
| public void setId(Integer id) { | ||
| this.id = id; | ||
| } | ||
|
|
||
| public ContainedEntity getContainedIndexedEmbedded() { | ||
| return containedIndexedEmbedded; | ||
| } | ||
|
|
||
| public Object getContainedIndexedEmbeddedWithCast() { | ||
| return containedIndexedEmbeddedWithCast; | ||
| } | ||
|
|
||
| public void setContainedIndexedEmbeddedWithCast(ContainedEntity containedIndexedEmbeddedWithCast) { | ||
| this.containedIndexedEmbeddedWithCast = containedIndexedEmbeddedWithCast; | ||
| } | ||
|
|
||
| public void setContainedIndexedEmbedded(ContainedEntity containedIndexedEmbedded) { | ||
| this.containedIndexedEmbedded = containedIndexedEmbedded; | ||
| } | ||
| } | ||
|
|
||
| @Entity(name = "contained") | ||
| public static class ContainedEntity { | ||
| @Id | ||
| private Integer id; | ||
|
|
||
| @OneToOne(mappedBy = "containedIndexedEmbeddedWithCast", targetEntity = ContainingEntity.class, | ||
| fetch = FetchType.LAZY) | ||
| @LazyGroup("containingAsIndexedEmbeddedWithCast") | ||
| private ContainingEntity containingAsIndexedEmbeddedWithCast; | ||
|
|
||
| @OneToOne(mappedBy = "containedIndexedEmbedded", fetch = FetchType.LAZY) | ||
| @LazyGroup("containingAsIndexedEmbedded") | ||
| private ContainingEntity containingAsIndexedEmbedded; | ||
|
|
||
| public Integer getId() { | ||
| return id; | ||
| } | ||
|
|
||
| public void setId(Integer id) { | ||
| this.id = id; | ||
| } | ||
|
|
||
| public ContainingEntity getContainingAsIndexedEmbedded() { | ||
| return containingAsIndexedEmbedded; | ||
| } | ||
|
|
||
| public Object getContainingAsIndexedEmbeddedWithCast() { | ||
| return containingAsIndexedEmbeddedWithCast; | ||
| } | ||
|
|
||
| public void setContainingAsIndexedEmbeddedWithCast(ContainingEntity containingAsIndexedEmbeddedWithCast) { | ||
| this.containingAsIndexedEmbeddedWithCast = containingAsIndexedEmbeddedWithCast; | ||
| } | ||
|
|
||
| public void setContainingAsIndexedEmbedded(ContainingEntity containingAsIndexedEmbedded) { | ||
| this.containingAsIndexedEmbedded = containingAsIndexedEmbedded; | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this might be only "hiding" the problem: we don't want this method to ignore the lazy attribute interceptor's initialized attributes set, as they might be read by other logic (e.g. field writes) instead of explicitly by this method.
I tried looking into this, and the
lazySelectLoadPlan.load()call seems to be doing just that, so I believe a better fix would be adapting the for loop that iterates through the lazy-group attributes to account for that.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍🏻 if you'll be looking into this, let's push that set variable initialization waaaaaaaaaaaaaay down after the
lazySelectLoadPlan.load. Because as the things are atm it's confusing at best, that theinitializedLazyAttributeNameschanges mid method 🙂