Permalink
Browse files

HHH-7371 Natural-id: ObjectNotFoundException when found entity is mar…

…ked as deleted
  • Loading branch information...
1 parent 8fa530a commit 4fe75d5a093902937c8247c6427566bdca474951 @stliu stliu committed Sep 5, 2012
@@ -45,6 +45,8 @@
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.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;
@@ -2605,7 +2608,16 @@ public final Object load() {
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;
}
}
@@ -2663,7 +2675,16 @@ public Object load(Object naturalIdValue) {
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;
}
}
}
@@ -160,6 +160,48 @@ public void testImmutableNaturalIdLifecycle() {
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() {

0 comments on commit 4fe75d5

Please sign in to comment.