-
-
Notifications
You must be signed in to change notification settings - Fork 3.8k
HHH-20515 Fix findMultiple overwriting session state with cache values #12880
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
Open
jjennnn
wants to merge
1
commit into
hibernate:main
Choose a base branch
from
jjennnn:HHH-20515
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
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
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
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
275 changes: 275 additions & 0 deletions
275
hibernate-core/src/test/java/org/hibernate/orm/test/cache/FindMultipleCacheTest.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,275 @@ | ||
| /* | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| * Copyright Red Hat Inc. and Hibernate Authors | ||
| */ | ||
| package org.hibernate.orm.test.cache; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| import org.hibernate.FindMultipleOption; | ||
| import org.hibernate.annotations.Cache; | ||
| import org.hibernate.annotations.CacheConcurrencyStrategy; | ||
| import org.hibernate.cfg.AvailableSettings; | ||
|
|
||
| import org.hibernate.testing.orm.junit.DomainModel; | ||
| import org.hibernate.testing.orm.junit.JiraKey; | ||
| import org.hibernate.testing.orm.junit.ServiceRegistry; | ||
| import org.hibernate.testing.orm.junit.SessionFactory; | ||
| import org.hibernate.testing.orm.junit.SessionFactoryScope; | ||
| import org.hibernate.testing.orm.junit.Setting; | ||
| import org.junit.jupiter.api.AfterEach; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| import jakarta.persistence.Basic; | ||
| import jakarta.persistence.Entity; | ||
| import jakarta.persistence.Id; | ||
|
|
||
| import static org.assertj.core.api.Assertions.assertThat; | ||
|
|
||
| @DomainModel( | ||
| annotatedClasses = { | ||
| FindMultipleCacheTest.CachedEntity.class, | ||
| FindMultipleCacheTest.NotCachedEntity.class | ||
| } | ||
| ) | ||
| @ServiceRegistry( | ||
| settings = { | ||
| @Setting(name = AvailableSettings.USE_SECOND_LEVEL_CACHE, value = "true"), | ||
| @Setting(name = AvailableSettings.USE_QUERY_CACHE, value = "true"), | ||
| @Setting(name = AvailableSettings.SHOW_SQL, value = "true"), | ||
| @Setting(name = AvailableSettings.FORMAT_SQL, value = "true"), | ||
| } | ||
| ) | ||
| @SessionFactory(generateStatistics = true) | ||
| @JiraKey("HHH-20515") | ||
| public class FindMultipleCacheTest { | ||
|
|
||
| @AfterEach | ||
| public void tearDown(SessionFactoryScope scope) { | ||
| scope.getSessionFactory().getSchemaManager().truncate(); | ||
| scope.getSessionFactory().getCache().evictAllRegions(); | ||
| } | ||
|
|
||
| @Test | ||
| public void testFindMultipleWithDisabledPreservesModifiedStateWith2LCPresent(SessionFactoryScope scope) { | ||
| scope.inTransaction( session -> { | ||
| session.persist( new CachedEntity( 1L, "originalName" ) ); | ||
| } ); | ||
|
|
||
| scope.inTransaction( session -> { | ||
| session.find( CachedEntity.class, 1L ); | ||
| } ); | ||
|
|
||
| scope.inTransaction( session -> { | ||
| CachedEntity entity = session.find( CachedEntity.class, 1L ); | ||
| entity.setName( "modifiedName" ); | ||
|
|
||
| List<CachedEntity> results = session.findMultiple( | ||
| CachedEntity.class, | ||
| List.of( 1L ), | ||
| FindMultipleOption.SessionCheckMode.DISABLED | ||
| ); | ||
|
|
||
| assertThat( results ).hasSize( 1 ); | ||
| assertThat( results.get( 0 ) ).isSameAs( entity ); | ||
| assertThat( results.get( 0 ).getName() ).isEqualTo( "modifiedName" ); | ||
| } ); | ||
| } | ||
|
|
||
| @Test | ||
| public void testFindMultipleWithDisabledAndUninitializedProxyInPersistenceContext( | ||
| SessionFactoryScope scope) { | ||
| scope.inTransaction( session -> { | ||
| session.persist( new CachedEntity( 1L, "name1" ) ); | ||
| } ); | ||
|
|
||
| scope.inTransaction( session -> { | ||
| session.find( CachedEntity.class, 1L ); | ||
| } ); | ||
|
|
||
| scope.inTransaction( session -> { | ||
| CachedEntity proxy = | ||
| session.getReference( CachedEntity.class, 1L ); | ||
|
|
||
| List<CachedEntity> results = session.findMultiple( | ||
| CachedEntity.class, | ||
| List.of( 1L ), | ||
| FindMultipleOption.SessionCheckMode.DISABLED | ||
|
|
||
| ); | ||
|
|
||
| assertThat( results ).hasSize( 1 ); | ||
| assertThat( results.get( 0 ) ).isSameAs( proxy ); | ||
| } ); | ||
| } | ||
|
|
||
| @Test | ||
| public void testFindMultipleWithEnabledPreservesModifiedStateWith2LCPresent(SessionFactoryScope scope) { | ||
| scope.inTransaction( session -> { | ||
| session.persist( new CachedEntity( 1L, "originalName" ) ); | ||
| } ); | ||
|
|
||
| scope.inTransaction( session -> { | ||
| session.find( CachedEntity.class, 1L ); | ||
| } ); | ||
|
|
||
| scope.inTransaction( session -> { | ||
| CachedEntity entity = session.find( CachedEntity.class, 1L ); | ||
| entity.setName( "modifiedName" ); | ||
|
|
||
| List<CachedEntity> results = session.findMultiple( | ||
| CachedEntity.class, | ||
| List.of( 1L ), | ||
| FindMultipleOption.SessionCheckMode.ENABLED | ||
| ); | ||
|
|
||
| assertThat( results ).hasSize( 1 ); | ||
| assertThat( results.get( 0 ) ).isSameAs( entity ); | ||
| assertThat( results.get( 0 ).getName() ).isEqualTo( "modifiedName" ); | ||
| } ); | ||
| } | ||
|
|
||
| @Test | ||
| public void testFindMultiplePreservesModifiedStateForNonCachedEntity(SessionFactoryScope scope) { | ||
| scope.inTransaction( session -> { | ||
| session.persist( new NotCachedEntity( 1L, "originalName" ) ); | ||
| } ); | ||
|
|
||
| scope.inTransaction( session -> { | ||
| NotCachedEntity entity = session.findMultiple( NotCachedEntity.class, List.of( 1L ) ).get( 0 ); | ||
| assertThat( entity.getName() ).isEqualTo( "originalName" ); | ||
| entity.setName( "modifiedName" ); | ||
|
|
||
| NotCachedEntity entity2 = session.findMultiple( NotCachedEntity.class, List.of( 1L ) ).get( 0 ); | ||
| assertThat( entity2 ).isSameAs( entity ); | ||
| assertThat( entity2.getName() ).isEqualTo( "modifiedName" ); | ||
| } ); | ||
| } | ||
|
|
||
| @Test | ||
| public void testFindMultipleRetrievesPersistedChangesFromCache(SessionFactoryScope scope) { | ||
| scope.inTransaction( session -> { | ||
| session.persist( new CachedEntity( 1L, "originalName" ) ); | ||
| } ); | ||
|
|
||
| scope.inTransaction( session -> { | ||
| CachedEntity entity = session.findMultiple( CachedEntity.class, List.of( 1L ) ).get( 0 ); | ||
| entity.setName( "persistedName" ); | ||
| } ); | ||
|
|
||
| scope.inTransaction( session -> { | ||
| CachedEntity entity = session.findMultiple( CachedEntity.class, List.of( 1L ) ).get( 0 ); | ||
| assertThat( entity.getName() ).isEqualTo( "persistedName" ); | ||
| } ); | ||
| } | ||
|
|
||
| @Test | ||
| public void testFindMultipleWithDisabledPreservesModifiedStateFromDb(SessionFactoryScope scope) { | ||
| scope.inTransaction( session -> { | ||
| session.persist( new CachedEntity( 1L, "originalName" ) ); | ||
| } ); | ||
|
|
||
| scope.inTransaction( session -> { | ||
| CachedEntity entity = session.find( CachedEntity.class, 1L ); | ||
|
|
||
| entity.setName( "modifiedName" ); | ||
|
|
||
| List<CachedEntity> results = session.findMultiple( | ||
| CachedEntity.class, | ||
| List.of( 1L ), | ||
| FindMultipleOption.SessionCheckMode.DISABLED | ||
| ); | ||
|
|
||
| assertThat( results ).hasSize( 1 ); | ||
| assertThat( results.get( 0 ) ).isSameAs( entity ); | ||
| assertThat( results.get( 0 ).getName() ).isEqualTo( "modifiedName" ); | ||
| } ); | ||
| } | ||
|
|
||
| @Test | ||
| public void testFindMultipleWithDisabledReturnsDeletedEntity(SessionFactoryScope scope) { | ||
| scope.inTransaction( session -> { | ||
| session.persist( new CachedEntity( 1L, "name1" ) ); | ||
| session.persist( new CachedEntity( 2L, "name2" ) ); | ||
| } ); | ||
|
|
||
| scope.inTransaction( session -> { | ||
| CachedEntity deletedEntity = | ||
| session.find( CachedEntity.class, 1L ); | ||
|
|
||
| session.remove( deletedEntity ); | ||
|
|
||
| List<CachedEntity> results = session.findMultiple( | ||
| CachedEntity.class, | ||
| List.of( 1L, 2L ), | ||
| FindMultipleOption.SessionCheckMode.DISABLED | ||
| ); | ||
|
|
||
| CachedEntity returnedEntity = results.get( 0 ); | ||
| assertThat( results ).hasSize( 2 ); | ||
|
|
||
| assertThat( results.get( 0 ) ).isNull(); | ||
| assertThat( results.get( 1 ) ).isNotNull(); | ||
| assertThat( results.get( 1 ).getName() ).isEqualTo( "name2" ); | ||
| } ); | ||
| } | ||
|
|
||
| @Entity(name = "CachedEntity") | ||
| @Cache(usage = CacheConcurrencyStrategy.READ_WRITE) | ||
| public static class CachedEntity { | ||
| @Id | ||
| private Long id; | ||
|
|
||
| @Basic | ||
| private String name; | ||
|
|
||
| public CachedEntity() { | ||
| } | ||
|
|
||
| public CachedEntity(Long id, String name) { | ||
| this.id = id; | ||
| this.name = name; | ||
| } | ||
|
|
||
| public Long getId() { | ||
| return id; | ||
| } | ||
|
|
||
| public String getName() { | ||
| return name; | ||
| } | ||
|
|
||
| public void setName(String name) { | ||
| this.name = name; | ||
| } | ||
| } | ||
|
|
||
| @Entity(name = "NotCachedEntity") | ||
| public static class NotCachedEntity { | ||
| @Id | ||
| private Long id; | ||
|
|
||
| @Basic | ||
| private String name; | ||
|
|
||
| public NotCachedEntity() { | ||
| } | ||
|
|
||
| public NotCachedEntity(Long id, String name) { | ||
| this.id = id; | ||
| this.name = name; | ||
| } | ||
|
|
||
| public Long getId() { | ||
| return id; | ||
| } | ||
|
|
||
| public String getName() { | ||
| return name; | ||
| } | ||
|
|
||
| public void setName(String name) { | ||
| this.name = name; | ||
| } | ||
| } | ||
| } | ||
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.
Uh oh!
There was an error while loading. Please reload this page.