Skip to content

Commit e269739

Browse files
committed
HHH-7513 check for natural id resolution in NaturalIdCacheKey
1 parent bb5e03c commit e269739

File tree

2 files changed

+22
-13
lines changed

2 files changed

+22
-13
lines changed

hibernate-core/src/main/java/org/hibernate/cache/spi/NaturalIdCacheKey.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import org.hibernate.internal.util.ValueHolder;
3434
import org.hibernate.internal.util.compare.EqualsHelper;
3535
import org.hibernate.persister.entity.EntityPersister;
36+
import org.hibernate.type.EntityType;
3637
import org.hibernate.type.Type;
3738

3839
/**
@@ -81,7 +82,15 @@ public NaturalIdCacheKey(
8182

8283
result = prime * result + (value != null ? type.getHashCode( value, factory ) : 0);
8384

84-
this.naturalIdValues[i] = type.disassemble( value, session, null );
85+
// The natural id may not be fully resolved in some situations. See HHH-7513 for one of them
86+
// (re-attaching a mutable natural id uses a database snapshot and hydration does not resolve associations).
87+
// TODO: The snapshot should probably be revisited at some point. Consider semi-resolving, hydrating, etc.
88+
if (type instanceof EntityType && type.getSemiResolvedType( factory ).getReturnedClass().isInstance( value )) {
89+
this.naturalIdValues[i] = (Serializable) value;
90+
}
91+
else {
92+
this.naturalIdValues[i] = type.disassemble( value, session, null );
93+
}
8594
}
8695

8796
this.hashCode = result;

hibernate-core/src/test/java/org/hibernate/test/naturalid/mutable/cached/CachedMutableNaturalIdTest.java

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -23,21 +23,19 @@
2323
*/
2424
package org.hibernate.test.naturalid.mutable.cached;
2525

26-
import java.io.Serializable;
26+
import static org.junit.Assert.assertEquals;
27+
import static org.junit.Assert.assertNotNull;
28+
import static org.junit.Assert.assertNull;
2729

28-
import org.junit.Test;
30+
import java.io.Serializable;
2931

3032
import org.hibernate.LockOptions;
3133
import org.hibernate.Session;
3234
import org.hibernate.cfg.Configuration;
3335
import org.hibernate.cfg.Environment;
34-
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
35-
import org.hibernate.testing.FailureExpected;
3636
import org.hibernate.testing.TestForIssue;
37-
38-
import static org.junit.Assert.assertEquals;
39-
import static org.junit.Assert.assertNotNull;
40-
import static org.junit.Assert.assertNull;
37+
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
38+
import org.junit.Test;
4139

4240
/**
4341
* Tests of mutable natural ids stored in second level cache
@@ -185,7 +183,6 @@ public void testNaturalIdChangeAfterResolveEntityFrom2LCache() {
185183
}
186184

187185
@Test
188-
@FailureExpected(jiraKey = "HHH-7513")
189186
public void testReattachementUnmodifiedInstance() {
190187
Session session = openSession();
191188
session.beginTransaction();
@@ -197,16 +194,19 @@ public void testReattachementUnmodifiedInstance() {
197194
b.assA = a;
198195
a.assB.add( b );
199196
session.getTransaction().commit();
200-
session.close();
197+
session.clear();
201198

202-
session = openSession();
203199
session.beginTransaction();
204200
session.buildLockRequest(LockOptions.NONE).lock(b); // HHH-7513 failure during reattachment
205201
session.delete(b.assA);
206202
session.delete(b);
207203

208204
session.getTransaction().commit();
209-
session.close();
205+
session.clear();
206+
207+
// true if the re-attachment worked
208+
assertEquals( session.createQuery( "FROM A" ).list().size(), 0 );
209+
assertEquals( session.createQuery( "FROM B" ).list().size(), 0 );
210210
}
211211

212212
}

0 commit comments

Comments
 (0)