HHH-7513 check for natural id resolution in NaturalIdCacheKey

This commit is contained in:
Brett Meyer 2013-10-07 17:50:59 -04:00
parent bb5e03cb64
commit e269739993
2 changed files with 22 additions and 13 deletions

View File

@ -33,6 +33,7 @@ import org.hibernate.engine.spi.SessionImplementor;
import org.hibernate.internal.util.ValueHolder; import org.hibernate.internal.util.ValueHolder;
import org.hibernate.internal.util.compare.EqualsHelper; import org.hibernate.internal.util.compare.EqualsHelper;
import org.hibernate.persister.entity.EntityPersister; import org.hibernate.persister.entity.EntityPersister;
import org.hibernate.type.EntityType;
import org.hibernate.type.Type; import org.hibernate.type.Type;
/** /**
@ -81,8 +82,16 @@ public class NaturalIdCacheKey implements Serializable {
result = prime * result + (value != null ? type.getHashCode( value, factory ) : 0); result = prime * result + (value != null ? type.getHashCode( value, factory ) : 0);
// The natural id may not be fully resolved in some situations. See HHH-7513 for one of them
// (re-attaching a mutable natural id uses a database snapshot and hydration does not resolve associations).
// TODO: The snapshot should probably be revisited at some point. Consider semi-resolving, hydrating, etc.
if (type instanceof EntityType && type.getSemiResolvedType( factory ).getReturnedClass().isInstance( value )) {
this.naturalIdValues[i] = (Serializable) value;
}
else {
this.naturalIdValues[i] = type.disassemble( value, session, null ); this.naturalIdValues[i] = type.disassemble( value, session, null );
} }
}
this.hashCode = result; this.hashCode = result;
initTransients(); initTransients();

View File

@ -23,21 +23,19 @@
*/ */
package org.hibernate.test.naturalid.mutable.cached; package org.hibernate.test.naturalid.mutable.cached;
import java.io.Serializable; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import org.junit.Test; import java.io.Serializable;
import org.hibernate.LockOptions; import org.hibernate.LockOptions;
import org.hibernate.Session; import org.hibernate.Session;
import org.hibernate.cfg.Configuration; import org.hibernate.cfg.Configuration;
import org.hibernate.cfg.Environment; import org.hibernate.cfg.Environment;
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
import org.hibernate.testing.FailureExpected;
import org.hibernate.testing.TestForIssue; import org.hibernate.testing.TestForIssue;
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
import static org.junit.Assert.assertEquals; import org.junit.Test;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
/** /**
* Tests of mutable natural ids stored in second level cache * Tests of mutable natural ids stored in second level cache
@ -185,7 +183,6 @@ public abstract class CachedMutableNaturalIdTest extends BaseCoreFunctionalTestC
} }
@Test @Test
@FailureExpected(jiraKey = "HHH-7513")
public void testReattachementUnmodifiedInstance() { public void testReattachementUnmodifiedInstance() {
Session session = openSession(); Session session = openSession();
session.beginTransaction(); session.beginTransaction();
@ -197,16 +194,19 @@ public abstract class CachedMutableNaturalIdTest extends BaseCoreFunctionalTestC
b.assA = a; b.assA = a;
a.assB.add( b ); a.assB.add( b );
session.getTransaction().commit(); session.getTransaction().commit();
session.close(); session.clear();
session = openSession();
session.beginTransaction(); session.beginTransaction();
session.buildLockRequest(LockOptions.NONE).lock(b); // HHH-7513 failure during reattachment session.buildLockRequest(LockOptions.NONE).lock(b); // HHH-7513 failure during reattachment
session.delete(b.assA); session.delete(b.assA);
session.delete(b); session.delete(b);
session.getTransaction().commit(); session.getTransaction().commit();
session.close(); session.clear();
// true if the re-attachment worked
assertEquals( session.createQuery( "FROM A" ).list().size(), 0 );
assertEquals( session.createQuery( "FROM B" ).list().size(), 0 );
} }
} }