From 77512d01a77ea1efb4ab6ef9e52343917b37745c Mon Sep 17 00:00:00 2001 From: Strong Liu Date: Wed, 5 Sep 2012 17:21:36 +0800 Subject: [PATCH] HHH-7371 Natural-id: ObjectNotFoundException when found entity is marked as deleted --- .../org/hibernate/internal/SessionImpl.java | 25 ++++++++++- .../ImmutableEntityNaturalIdTest.java | 42 +++++++++++++++++++ 2 files changed, 65 insertions(+), 2 deletions(-) diff --git a/hibernate-core/src/main/java/org/hibernate/internal/SessionImpl.java b/hibernate-core/src/main/java/org/hibernate/internal/SessionImpl.java index 2a30488f6d..499a162af4 100644 --- a/hibernate-core/src/main/java/org/hibernate/internal/SessionImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/internal/SessionImpl.java @@ -45,6 +45,8 @@ import java.util.List; import java.util.Map; import java.util.Set; +import javax.persistence.EntityNotFoundException; + import org.jboss.logging.Logger; import org.hibernate.AssertionFailure; @@ -64,6 +66,7 @@ import org.hibernate.LockOptions; import org.hibernate.MappingException; import org.hibernate.NaturalIdLoadAccess; import org.hibernate.ObjectDeletedException; +import org.hibernate.ObjectNotFoundException; import org.hibernate.Query; import org.hibernate.QueryException; import org.hibernate.ReplicationMode; @@ -2583,7 +2586,16 @@ public final class SessionImpl extends AbstractSessionImpl implements EventSourc if ( entityId == null ) { return null; } - return this.getIdentifierLoadAccess().load( entityId ); + try { + return this.getIdentifierLoadAccess().load( entityId ); + } + catch (EntityNotFoundException enf) { + // OK + } + catch (ObjectNotFoundException nf) { + // OK + } + return null; } } @@ -2641,7 +2653,16 @@ public final class SessionImpl extends AbstractSessionImpl implements EventSourc if ( entityId == null ) { return null; } - return this.getIdentifierLoadAccess().load( entityId ); + try { + return this.getIdentifierLoadAccess().load( entityId ); + } + catch (EntityNotFoundException enf) { + // OK + } + catch (ObjectNotFoundException nf) { + // OK + } + return null; } } } diff --git a/hibernate-core/src/test/java/org/hibernate/test/naturalid/immutableentity/ImmutableEntityNaturalIdTest.java b/hibernate-core/src/test/java/org/hibernate/test/naturalid/immutableentity/ImmutableEntityNaturalIdTest.java index cc79c28a3b..b103046511 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/naturalid/immutableentity/ImmutableEntityNaturalIdTest.java +++ b/hibernate-core/src/test/java/org/hibernate/test/naturalid/immutableentity/ImmutableEntityNaturalIdTest.java @@ -160,6 +160,48 @@ public class ImmutableEntityNaturalIdTest extends BaseCoreFunctionalTestCase { tx.rollback(); 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 protected Class[] getAnnotatedClasses() {