Author: gbadner
Date: 2010-04-30 15:33:42 -0400 (Fri, 30 Apr 2010)
New Revision: 19331
Modified:
core/branches/Branch_3_5/core/src/main/java/org/hibernate/cache/StandardQueryCache.java
core/branches/Branch_3_5/core/src/main/java/org/hibernate/impl/SessionFactoryImpl.java
core/branches/Branch_3_5/core/src/main/java/org/hibernate/proxy/EntityNotFoundDelegate.java
core/branches/Branch_3_5/entitymanager/src/main/java/org/hibernate/ejb/Ejb3Configuration.java
core/branches/Branch_3_5/testsuite/src/test/java/org/hibernate/test/jpa/AbstractJPATest.java
core/branches/Branch_3_5/testsuite/src/test/java/org/hibernate/test/naturalid/immutable/ImmutableNaturalIdTest.java
core/branches/Branch_3_5/testsuite/src/test/java/org/hibernate/test/naturalid/mutable/MutableNaturalIdTest.java
Log:
HHH-3478 : StandardQueryCache.get() does not handle EntityNotFoundException for natural
key lookups
Modified:
core/branches/Branch_3_5/core/src/main/java/org/hibernate/cache/StandardQueryCache.java
===================================================================
---
core/branches/Branch_3_5/core/src/main/java/org/hibernate/cache/StandardQueryCache.java 2010-04-30
15:53:27 UTC (rev 19330)
+++
core/branches/Branch_3_5/core/src/main/java/org/hibernate/cache/StandardQueryCache.java 2010-04-30
19:33:42 UTC (rev 19331)
@@ -162,8 +162,10 @@
);
}
}
- catch ( UnresolvableObjectException uoe ) {
- if ( isNaturalKeyLookup ) {
+ catch ( RuntimeException ex ) {
+ if ( isNaturalKeyLookup &&
+ ( UnresolvableObjectException.class.isInstance( ex ) ||
+ session.getFactory().getEntityNotFoundDelegate().isEntityNotFoundException( ex ) )
) {
//TODO: not really completely correct, since
// the uoe could occur while resolving
// associations, leaving the PC in an
@@ -173,7 +175,7 @@
return null;
}
else {
- throw uoe;
+ throw ex;
}
}
}
Modified:
core/branches/Branch_3_5/core/src/main/java/org/hibernate/impl/SessionFactoryImpl.java
===================================================================
---
core/branches/Branch_3_5/core/src/main/java/org/hibernate/impl/SessionFactoryImpl.java 2010-04-30
15:53:27 UTC (rev 19330)
+++
core/branches/Branch_3_5/core/src/main/java/org/hibernate/impl/SessionFactoryImpl.java 2010-04-30
19:33:42 UTC (rev 19331)
@@ -446,6 +446,9 @@
public void handleEntityNotFound(String entityName, Serializable id) {
throw new ObjectNotFoundException( id, entityName );
}
+ public boolean isEntityNotFoundException(RuntimeException exception) {
+ return ObjectNotFoundException.class.isInstance( exception );
+ }
};
}
this.entityNotFoundDelegate = entityNotFoundDelegate;
Modified:
core/branches/Branch_3_5/core/src/main/java/org/hibernate/proxy/EntityNotFoundDelegate.java
===================================================================
---
core/branches/Branch_3_5/core/src/main/java/org/hibernate/proxy/EntityNotFoundDelegate.java 2010-04-30
15:53:27 UTC (rev 19330)
+++
core/branches/Branch_3_5/core/src/main/java/org/hibernate/proxy/EntityNotFoundDelegate.java 2010-04-30
19:33:42 UTC (rev 19331)
@@ -33,4 +33,5 @@
*/
public interface EntityNotFoundDelegate {
public void handleEntityNotFound(String entityName, Serializable id);
+ public boolean isEntityNotFoundException(RuntimeException exception);
}
Modified:
core/branches/Branch_3_5/entitymanager/src/main/java/org/hibernate/ejb/Ejb3Configuration.java
===================================================================
---
core/branches/Branch_3_5/entitymanager/src/main/java/org/hibernate/ejb/Ejb3Configuration.java 2010-04-30
15:53:27 UTC (rev 19330)
+++
core/branches/Branch_3_5/entitymanager/src/main/java/org/hibernate/ejb/Ejb3Configuration.java 2010-04-30
19:33:42 UTC (rev 19331)
@@ -70,6 +70,7 @@
import org.hibernate.Interceptor;
import org.hibernate.MappingException;
import org.hibernate.MappingNotFoundException;
+import org.hibernate.ObjectNotFoundException;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.cfg.Configuration;
@@ -132,6 +133,9 @@
public void handleEntityNotFound(String entityName, Serializable id) {
throw new EntityNotFoundException("Unable to find " + entityName + "
with id " + id);
}
+ public boolean isEntityNotFoundException(RuntimeException exception) {
+ return EntityNotFoundException.class.isInstance( exception );
+ }
}
static {
Modified:
core/branches/Branch_3_5/testsuite/src/test/java/org/hibernate/test/jpa/AbstractJPATest.java
===================================================================
---
core/branches/Branch_3_5/testsuite/src/test/java/org/hibernate/test/jpa/AbstractJPATest.java 2010-04-30
15:53:27 UTC (rev 19330)
+++
core/branches/Branch_3_5/testsuite/src/test/java/org/hibernate/test/jpa/AbstractJPATest.java 2010-04-30
19:33:42 UTC (rev 19331)
@@ -55,6 +55,9 @@
public void handleEntityNotFound(String entityName, Serializable id) {
throw new EntityNotFoundException( entityName, id );
}
+ public boolean isEntityNotFoundException(RuntimeException exception) {
+ return EntityNotFoundException.class.isInstance( exception );
+ }
}
/**
Modified:
core/branches/Branch_3_5/testsuite/src/test/java/org/hibernate/test/naturalid/immutable/ImmutableNaturalIdTest.java
===================================================================
---
core/branches/Branch_3_5/testsuite/src/test/java/org/hibernate/test/naturalid/immutable/ImmutableNaturalIdTest.java 2010-04-30
15:53:27 UTC (rev 19330)
+++
core/branches/Branch_3_5/testsuite/src/test/java/org/hibernate/test/naturalid/immutable/ImmutableNaturalIdTest.java 2010-04-30
19:33:42 UTC (rev 19331)
@@ -155,4 +155,99 @@
s.getTransaction().commit();
s.close();
}
+
+ public void testNaturalIdDeleteUsingCache() {
+ Session s = openSession();
+ s.beginTransaction();
+ User u = new User( "steve", "superSecret" );
+ s.persist( u );
+ s.getTransaction().commit();
+ s.close();
+
+ getSessions().getStatistics().clear();
+
+ s = openSession();
+ s.beginTransaction();
+ u = ( User ) s.createCriteria( User.class )
+ .add( Restrictions.naturalId().set( "userName", "steve" ) )
+ .setCacheable( true )
+ .uniqueResult();
+ assertNotNull( u );
+ s.getTransaction().commit();
+ s.close();
+
+ assertEquals( getSessions().getStatistics().getQueryExecutionCount(), 1 );
+ assertEquals( getSessions().getStatistics().getQueryCacheHitCount(), 0 );
+ assertEquals( getSessions().getStatistics().getQueryCachePutCount(), 1 );
+
+ getSessions().getStatistics().clear();
+
+ s = openSession();
+ s.beginTransaction();
+ u = ( User ) s.createCriteria( User.class )
+ .add( Restrictions.naturalId().set( "userName", "steve" ) )
+ .setCacheable( true )
+ .uniqueResult();
+ assertNotNull( u );
+ assertEquals( getSessions().getStatistics().getQueryExecutionCount(), 0 );
+ assertEquals( getSessions().getStatistics().getQueryCacheHitCount(), 1 );
+
+ s.delete( u );
+
+ s.getTransaction().commit();
+ s.close();
+
+ s = openSession();
+ s.beginTransaction();
+ u = ( User ) s.createCriteria( User.class )
+ .add( Restrictions.naturalId().set( "userName", "steve" ) )
+ .setCacheable( true )
+ .uniqueResult();
+ assertNull( u );
+ s.getTransaction().commit();
+ s.close();
+ }
+
+ public void testNaturalIdRecreateUsingCache() {
+ testNaturalIdDeleteUsingCache();
+
+ Session s = openSession();
+ s.beginTransaction();
+ User u = new User( "steve", "superSecret" );
+ s.persist( u );
+ s.getTransaction().commit();
+ s.close();
+
+ getSessions().getStatistics().clear();
+
+ s = openSession();
+ s.beginTransaction();
+ u = ( User ) s.createCriteria( User.class )
+ .add( Restrictions.naturalId().set( "userName", "steve" ) )
+ .setCacheable( true )
+ .uniqueResult();
+ assertNotNull( u );
+
+ assertEquals( getSessions().getStatistics().getQueryExecutionCount(), 1 );
+ assertEquals( getSessions().getStatistics().getQueryCacheHitCount(), 0 );
+ assertEquals( getSessions().getStatistics().getQueryCachePutCount(), 1 );
+
+ getSessions().getStatistics().clear();
+
+ s = openSession();
+ s.beginTransaction();
+ u = ( User ) s.createCriteria( User.class )
+ .add( Restrictions.naturalId().set( "userName", "steve" ) )
+ .setCacheable( true )
+ .uniqueResult();
+ assertNotNull( u );
+ assertEquals( getSessions().getStatistics().getQueryExecutionCount(), 0 );
+ assertEquals( getSessions().getStatistics().getQueryCacheHitCount(), 1 );
+
+ s.delete( u );
+
+ s.getTransaction().commit();
+ s.close();
+ }
+
}
Modified:
core/branches/Branch_3_5/testsuite/src/test/java/org/hibernate/test/naturalid/mutable/MutableNaturalIdTest.java
===================================================================
---
core/branches/Branch_3_5/testsuite/src/test/java/org/hibernate/test/naturalid/mutable/MutableNaturalIdTest.java 2010-04-30
15:53:27 UTC (rev 19330)
+++
core/branches/Branch_3_5/testsuite/src/test/java/org/hibernate/test/naturalid/mutable/MutableNaturalIdTest.java 2010-04-30
19:33:42 UTC (rev 19331)
@@ -244,6 +244,115 @@
s.close();
}
+ public void testNaturalIdDeleteUsingCache() {
+ Session s = openSession();
+ s.beginTransaction();
+ User u = new User( "steve", "hb", "superSecret" );
+ s.persist( u );
+ s.getTransaction().commit();
+ s.close();
+
+ getSessions().getStatistics().clear();
+
+ s = openSession();
+ s.beginTransaction();
+ u = ( User ) s.createCriteria( User.class )
+ .add( Restrictions.naturalId()
+ .set("name", "steve")
+ .set("org", "hb")
+ )
+ .setCacheable( true )
+ .uniqueResult();
+ assertNotNull( u );
+ s.getTransaction().commit();
+ s.close();
+
+ assertEquals( getSessions().getStatistics().getQueryExecutionCount(), 1 );
+ assertEquals( getSessions().getStatistics().getQueryCacheHitCount(), 0 );
+ assertEquals( getSessions().getStatistics().getQueryCachePutCount(), 1 );
+
+ getSessions().getStatistics().clear();
+
+ s = openSession();
+ s.beginTransaction();
+ u = ( User ) s.createCriteria( User.class )
+ .add( Restrictions.naturalId()
+ .set("name", "steve")
+ .set("org", "hb")
+ )
+ .setCacheable( true )
+ .uniqueResult();
+ assertNotNull( u );
+ assertEquals( getSessions().getStatistics().getQueryExecutionCount(), 0 );
+ assertEquals( getSessions().getStatistics().getQueryCacheHitCount(), 1 );
+
+ s.delete( u );
+
+ s.getTransaction().commit();
+ s.close();
+
+ s = openSession();
+ s.beginTransaction();
+ u = ( User ) s.createCriteria( User.class )
+ .add( Restrictions.naturalId()
+ .set("name", "steve")
+ .set("org", "hb")
+ )
+ .setCacheable( true )
+ .uniqueResult();
+ assertNull( u );
+ s.getTransaction().commit();
+ s.close();
+ }
+
+ public void testNaturalIdRecreateUsingCache() {
+ testNaturalIdDeleteUsingCache();
+
+ Session s = openSession();
+ s.beginTransaction();
+ User u = new User( "steve", "hb", "superSecret" );
+ s.persist( u );
+ s.getTransaction().commit();
+ s.close();
+
+ getSessions().getStatistics().clear();
+
+ s = openSession();
+ s.beginTransaction();
+ u = ( User ) s.createCriteria( User.class )
+ .add( Restrictions.naturalId()
+ .set("name", "steve")
+ .set("org", "hb")
+ )
+ .setCacheable( true )
+ .uniqueResult();
+ assertNotNull( u );
+
+ assertEquals( getSessions().getStatistics().getQueryExecutionCount(), 1 );
+ assertEquals( getSessions().getStatistics().getQueryCacheHitCount(), 0 );
+ assertEquals( getSessions().getStatistics().getQueryCachePutCount(), 1 );
+
+ getSessions().getStatistics().clear();
+
+ s = openSession();
+ s.beginTransaction();
+ u = ( User ) s.createCriteria( User.class )
+ .add( Restrictions.naturalId()
+ .set("name", "steve")
+ .set("org", "hb")
+ )
+ .setCacheable( true )
+ .uniqueResult();
+ assertNotNull( u );
+ assertEquals( getSessions().getStatistics().getQueryExecutionCount(), 0 );
+ assertEquals( getSessions().getStatistics().getQueryCacheHitCount(), 1 );
+
+ s.delete( u );
+
+ s.getTransaction().commit();
+ s.close();
+ }
+
public void testQuerying() throws Exception {
Session s = openSession();
Transaction t = s.beginTransaction();