HHH-7371 Natural-id: ObjectNotFoundException when found entity is marked as deleted

This commit is contained in:
Strong Liu 2012-09-05 17:21:36 +08:00
parent a608bc3b87
commit 77512d01a7
2 changed files with 65 additions and 2 deletions

View File

@ -45,6 +45,8 @@ import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
import javax.persistence.EntityNotFoundException;
import org.jboss.logging.Logger; import org.jboss.logging.Logger;
import org.hibernate.AssertionFailure; import org.hibernate.AssertionFailure;
@ -64,6 +66,7 @@ import org.hibernate.LockOptions;
import org.hibernate.MappingException; import org.hibernate.MappingException;
import org.hibernate.NaturalIdLoadAccess; import org.hibernate.NaturalIdLoadAccess;
import org.hibernate.ObjectDeletedException; import org.hibernate.ObjectDeletedException;
import org.hibernate.ObjectNotFoundException;
import org.hibernate.Query; import org.hibernate.Query;
import org.hibernate.QueryException; import org.hibernate.QueryException;
import org.hibernate.ReplicationMode; import org.hibernate.ReplicationMode;
@ -2583,8 +2586,17 @@ public final class SessionImpl extends AbstractSessionImpl implements EventSourc
if ( entityId == null ) { if ( entityId == null ) {
return null; return null;
} }
try {
return this.getIdentifierLoadAccess().load( entityId ); return this.getIdentifierLoadAccess().load( entityId );
} }
catch (EntityNotFoundException enf) {
// OK
}
catch (ObjectNotFoundException nf) {
// OK
}
return null;
}
} }
private class SimpleNaturalIdLoadAccessImpl extends BaseNaturalIdLoadAccessImpl implements SimpleNaturalIdLoadAccess { private class SimpleNaturalIdLoadAccessImpl extends BaseNaturalIdLoadAccessImpl implements SimpleNaturalIdLoadAccess {
@ -2641,7 +2653,16 @@ public final class SessionImpl extends AbstractSessionImpl implements EventSourc
if ( entityId == null ) { if ( entityId == null ) {
return null; return null;
} }
try {
return this.getIdentifierLoadAccess().load( entityId ); return this.getIdentifierLoadAccess().load( entityId );
} }
catch (EntityNotFoundException enf) {
// OK
}
catch (ObjectNotFoundException nf) {
// OK
}
return null;
}
} }
} }

View File

@ -161,6 +161,48 @@ public class ImmutableEntityNaturalIdTest extends BaseCoreFunctionalTestCase {
s.close(); s.close();
} }
@Test
@TestForIssue( jiraKey = "HHH-7371" )
public void testImmutableNaturalIdLifecycle2() {
Building b1 = new Building();
b1.setName( "Computer Science" );
b1.setAddress( "1210 W. Dayton St." );
b1.setCity( "Madison" );
b1.setState( "WI" );
Session s = openSession();
Transaction tx = s.beginTransaction();
s.persist( b1 );
tx.commit();
s.close();
s = openSession();
tx = s.beginTransaction();
NaturalIdLoadAccess naturalIdLoader = s.byNaturalId( Building.class );
naturalIdLoader.using( "address", "1210 W. Dayton St." ).using( "city", "Madison" ).using( "state", "WI" );
Building building = (Building) naturalIdLoader.getReference();
assertNotNull( building );
s.delete( building );
building = (Building) naturalIdLoader.load();
//org.hibernate.ObjectNotFoundException: No row with the given identifier exists: [org.hibernate.test.naturalid.immutableentity.Building#1]
// at org.hibernate.internal.SessionFactoryImpl$1$1.handleEntityNotFound(SessionFactoryImpl.java:247)
// at org.hibernate.event.internal.DefaultLoadEventListener.returnNarrowedProxy(DefaultLoadEventListener.java:282)
// at org.hibernate.event.internal.DefaultLoadEventListener.proxyOrLoad(DefaultLoadEventListener.java:248)
// at org.hibernate.event.internal.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:148)
// at org.hibernate.internal.SessionImpl.fireLoad(SessionImpl.java:1079)
// at org.hibernate.internal.SessionImpl.access$13(SessionImpl.java:1075)
// at org.hibernate.internal.SessionImpl$IdentifierLoadAccessImpl.load(SessionImpl.java:2425)
// at org.hibernate.internal.SessionImpl$NaturalIdLoadAccessImpl.load(SessionImpl.java:2586)
// at org.hibernate.test.naturalid.immutableentity.ImmutableEntityNaturalIdTest.testImmutableNaturalIdLifecycle2(ImmutableEntityNaturalIdTest.java:188)
assertNull( building );
tx.commit();
s.close();
}
@Override @Override
protected Class[] getAnnotatedClasses() { protected Class[] getAnnotatedClasses() {
return new Class[] { return new Class[] {