Skip to content

Commit cbcdd23

Browse files
committed
HHH-7287 - Problem in caching proper natural-id-values when obtaining result by naturalIdQuery
1 parent f144b9f commit cbcdd23

File tree

1 file changed

+39
-1
lines changed

1 file changed

+39
-1
lines changed

hibernate-core/src/test/java/org/hibernate/orm/test/mapping/naturalid/mutable/MutableNaturalIdTest.java

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import static org.junit.jupiter.api.Assertions.assertNotNull;
3030
import static org.junit.jupiter.api.Assertions.assertNotSame;
3131
import static org.junit.jupiter.api.Assertions.assertNull;
32+
import static org.junit.jupiter.api.Assertions.assertSame;
3233

3334
/**
3435
* @author Gavin King
@@ -58,7 +59,7 @@ public void testNaturalIdNullability(SessionFactoryScope scope) {
5859

5960
@AfterEach
6061
public void dropTestData(SessionFactoryScope scope) {
61-
scope.getSessionFactory().getSchemaManager().truncate();
62+
scope.dropData();
6263
}
6364

6465
@Test
@@ -362,4 +363,41 @@ public void testEviction(SessionFactoryScope scope) {
362363
}
363364
);
364365
}
366+
367+
@Test
368+
@JiraKey("HHH-7287")
369+
public void testModificationInOtherSession(SessionFactoryScope factoryScope) {
370+
factoryScope.inTransaction( (session) -> {
371+
User u = new User( "gavin", "hb", "secret" );
372+
session.persist( u );
373+
} );
374+
375+
// Use transactionless session
376+
factoryScope.inSession( (session) -> {
377+
// this loads the state into this `session`
378+
var byNaturalId = session.byNaturalId( User.class ).using( "name", "gavin" ).using( "org", "hb" ).load();
379+
assertNotNull( byNaturalId );
380+
381+
// CHANGE natural-id values in another session
382+
factoryScope.inTransaction( (otherSession) -> {
383+
var u = otherSession.find( User.class, 1 );
384+
u.setOrg( "zz" );
385+
} );
386+
// CHANGE APPLIED
387+
388+
byNaturalId = session.byNaturalId( User.class )
389+
.using( "name", "gavin" )
390+
.using( "org", "hb" ).load();
391+
assertNotNull( byNaturalId );
392+
393+
// the internal query will 'see' the new values, because isolation level < SERIALIZABLE
394+
var byNaturalId2 = session.byNaturalId( User.class )
395+
.using( "name", "gavin" )
396+
.using( "org", "zz" ).load();
397+
assertSame( byNaturalId, byNaturalId2 );
398+
399+
// this fails, that's the bug
400+
assertNotNull( session.byNaturalId( User.class ).using( "name", "gavin" ).using( "org", "hb" ).load());
401+
} );
402+
}
365403
}

0 commit comments

Comments
 (0)