|
From: <hib...@li...> - 2006-05-02 18:58:15
|
Author: ste...@jb...
Date: 2006-05-02 14:55:45 -0400 (Tue, 02 May 2006)
New Revision: 9855
Added:
trunk/Hibernate3/src/org/hibernate/proxy/EntityNotFoundDelegate.java
trunk/Hibernate3/test/org/hibernate/test/ejb3/proxy/
trunk/Hibernate3/test/org/hibernate/test/ejb3/proxy/Ejb3ProxyTest.java
Modified:
trunk/Hibernate3/src/org/hibernate/ObjectNotFoundException.java
trunk/Hibernate3/src/org/hibernate/cfg/Configuration.java
trunk/Hibernate3/src/org/hibernate/engine/SessionFactoryImplementor.java
trunk/Hibernate3/src/org/hibernate/event/def/DefaultLoadEventListener.java
trunk/Hibernate3/src/org/hibernate/impl/SessionFactoryImpl.java
trunk/Hibernate3/src/org/hibernate/impl/SessionImpl.java
trunk/Hibernate3/src/org/hibernate/proxy/AbstractLazyInitializer.java
trunk/Hibernate3/test/org/hibernate/test/ejb3/EJB3Suite.java
trunk/Hibernate3/test/org/hibernate/test/proxy/ProxyTest.java
Log:
HHH-1591 - hook for ejb3 ENFE
HHH-1714 - get() semantics
Modified: trunk/Hibernate3/src/org/hibernate/ObjectNotFoundException.java
===================================================================
--- trunk/Hibernate3/src/org/hibernate/ObjectNotFoundException.java 2006-05-02 15:11:17 UTC (rev 9854)
+++ trunk/Hibernate3/src/org/hibernate/ObjectNotFoundException.java 2006-05-02 18:55:45 UTC (rev 9855)
@@ -21,17 +21,4 @@
public ObjectNotFoundException(Serializable identifier, String clazz) {
super(identifier, clazz);
}
-
- public static void throwIfNull(Object o, Serializable id, String clazz)
- throws ObjectNotFoundException {
- if (o==null) throw new ObjectNotFoundException(id, clazz);
- }
-
}
-
-
-
-
-
-
-
Modified: trunk/Hibernate3/src/org/hibernate/cfg/Configuration.java
===================================================================
--- trunk/Hibernate3/src/org/hibernate/cfg/Configuration.java 2006-05-02 15:11:17 UTC (rev 9854)
+++ trunk/Hibernate3/src/org/hibernate/cfg/Configuration.java 2006-05-02 18:55:45 UTC (rev 9855)
@@ -36,6 +36,7 @@
import org.hibernate.Interceptor;
import org.hibernate.MappingException;
import org.hibernate.SessionFactory;
+import org.hibernate.proxy.EntityNotFoundDelegate;
import org.hibernate.dialect.Dialect;
import org.hibernate.dialect.MySQLDialect;
import org.hibernate.engine.FilterDefinition;
@@ -136,6 +137,7 @@
private Interceptor interceptor;
private Properties properties;
private EntityResolver entityResolver;
+ private EntityNotFoundDelegate entityNotFoundDelegate;
private transient XMLHelper xmlHelper;
protected transient Map typeDefs;
@@ -236,6 +238,27 @@
}
/**
+ * Retrieve the user-supplied delegate to handle non-existent entity
+ * scenarios. May be null.
+ *
+ * @return The user-supplied delegate
+ */
+ public EntityNotFoundDelegate getEntityNotFoundDelegate() {
+ return entityNotFoundDelegate;
+ }
+
+ /**
+ * Specify a user-supplied delegate to be used to handle scenarios where an entity could not be
+ * located by specified id. This is mainly intended for EJB3 implementations to be able to
+ * control how proxy initialization errors should be handled...
+ *
+ * @param entityNotFoundDelegate The delegate to use
+ */
+ public void setEntityNotFoundDelegate(EntityNotFoundDelegate entityNotFoundDelegate) {
+ this.entityNotFoundDelegate = entityNotFoundDelegate;
+ }
+
+ /**
* Read mappings from a particular XML file
*
* @param xmlFile a path to a file
@@ -343,7 +366,9 @@
* @param xml an XML string
*/
public Configuration addXML(String xml) throws MappingException {
- if ( log.isDebugEnabled() ) log.debug( "Mapping XML:\n" + xml );
+ if ( log.isDebugEnabled() ) {
+ log.debug( "Mapping XML:\n" + xml );
+ }
try {
List errors = new ArrayList();
org.dom4j.Document doc = xmlHelper.createSAXReader( "XML String", errors, entityResolver )
@@ -365,7 +390,9 @@
* @param url
*/
public Configuration addURL(URL url) throws MappingException {
- if ( log.isDebugEnabled() ) log.debug( "Reading mapping document from URL:" + url );
+ if ( log.isDebugEnabled() ) {
+ log.debug( "Reading mapping document from URL:" + url );
+ }
try {
addInputStream( url.openStream() );
}
@@ -381,7 +408,9 @@
* @param doc a DOM document
*/
public Configuration addDocument(Document doc) throws MappingException {
- if ( log.isDebugEnabled() ) log.debug( "Mapping document:\n" + doc );
+ if ( log.isDebugEnabled() ) {
+ log.debug( "Mapping document:\n" + doc );
+ }
add( xmlHelper.createDOMReader().read( doc ) );
return this;
}
@@ -559,7 +588,9 @@
finally {
try {
- if (jarFile!=null) jarFile.close();
+ if ( jarFile != null ) {
+ jarFile.close();
+ }
}
catch (IOException ioe) {
log.error("could not close jar", ioe);
@@ -862,7 +893,9 @@
defaultCatalog,
defaultSchema
);
- while ( subiter.hasNext() ) script.add( subiter.next() );
+ while ( subiter.hasNext() ) {
+ script.add( subiter.next() );
+ }
}
Iterator comments = table.sqlCommentStrings( dialect, defaultCatalog, defaultSchema );
@@ -1413,12 +1446,16 @@
Element sfNode = doc.getRootElement().element( "session-factory" );
String name = sfNode.attributeValue( "name" );
- if ( name != null ) properties.setProperty( Environment.SESSION_FACTORY_NAME, name );
+ if ( name != null ) {
+ properties.setProperty( Environment.SESSION_FACTORY_NAME, name );
+ }
addProperties( sfNode );
parseSessionFactory( sfNode, name );
Element secNode = doc.getRootElement().element( "security" );
- if ( secNode != null ) parseSecurity( secNode );
+ if ( secNode != null ) {
+ parseSecurity( secNode );
+ }
log.info( "Configured SessionFactory: " + name );
log.debug( "properties: " + properties );
@@ -1527,7 +1564,9 @@
private void parseListener(Element element) {
String type = element.attributeValue( "type" );
- if ( type == null ) throw new MappingException( "No type specified for listener" );
+ if ( type == null ) {
+ throw new MappingException( "No type specified for listener" );
+ }
String impl = element.attributeValue( "class" );
log.debug( "Event listener: " + type + "=" + impl );
setListeners( type, new String[]{impl} );
@@ -1835,7 +1874,9 @@
void setCacheConcurrencyStrategy(String clazz, String concurrencyStrategy, String region, boolean includeLazy)
throws MappingException {
RootClass rootClass = getRootClassMapping( clazz );
- if ( rootClass == null ) throw new MappingException( "Cannot cache an unknown entity: " + clazz );
+ if ( rootClass == null ) {
+ throw new MappingException( "Cannot cache an unknown entity: " + clazz );
+ }
rootClass.setCacheConcurrencyStrategy( concurrencyStrategy );
rootClass.setCacheRegionName( region );
rootClass.setLazyPropertiesCacheable( includeLazy );
@@ -1858,7 +1899,9 @@
public void setCollectionCacheConcurrencyStrategy(String collectionRole, String concurrencyStrategy, String region)
throws MappingException {
Collection collection = getCollectionMapping( collectionRole );
- if ( collection == null ) throw new MappingException( "Cannot cache an unknown collection: " + collectionRole );
+ if ( collection == null ) {
+ throw new MappingException( "Cannot cache an unknown collection: " + collectionRole );
+ }
collection.setCacheConcurrencyStrategy( concurrencyStrategy );
collection.setCacheRegionName( region );
}
@@ -1928,7 +1971,9 @@
if ( pc == null ) {
throw new MappingException( "persistent class not known: " + persistentClass );
}
- if ( !pc.hasIdentifierProperty() ) return null;
+ if ( !pc.hasIdentifierProperty() ) {
+ return null;
+ }
return pc.getIdentifierProperty().getName();
}
Modified: trunk/Hibernate3/src/org/hibernate/engine/SessionFactoryImplementor.java
===================================================================
--- trunk/Hibernate3/src/org/hibernate/engine/SessionFactoryImplementor.java 2006-05-02 15:11:17 UTC (rev 9854)
+++ trunk/Hibernate3/src/org/hibernate/engine/SessionFactoryImplementor.java 2006-05-02 18:55:45 UTC (rev 9855)
@@ -12,6 +12,7 @@
import org.hibernate.MappingException;
import org.hibernate.SessionFactory;
import org.hibernate.ConnectionReleaseMode;
+import org.hibernate.proxy.EntityNotFoundDelegate;
import org.hibernate.engine.query.QueryPlanCache;
import org.hibernate.persister.collection.CollectionPersister;
import org.hibernate.persister.entity.EntityPersister;
@@ -161,4 +162,6 @@
* @return set of all the collection roles in which the given entityName participates.
*/
public Set getCollectionRolesByEntityParticipant(String entityName);
+
+ public EntityNotFoundDelegate getEntityNotFoundDelegate();
}
Modified: trunk/Hibernate3/src/org/hibernate/event/def/DefaultLoadEventListener.java
===================================================================
--- trunk/Hibernate3/src/org/hibernate/event/def/DefaultLoadEventListener.java 2006-05-02 15:11:17 UTC (rev 9854)
+++ trunk/Hibernate3/src/org/hibernate/event/def/DefaultLoadEventListener.java 2006-05-02 18:55:45 UTC (rev 9855)
@@ -50,7 +50,6 @@
* Handle the given load event.
*
* @param event The load event to be handled.
- * @return The result (i.e., the loaded entity).
* @throws HibernateException
*/
public void onLoad(LoadEvent event, LoadEventListener.LoadType loadType) throws HibernateException {
@@ -125,7 +124,9 @@
boolean isOptionalInstance = event.getInstanceToLoad() != null;
if ( !options.isAllowNulls() || isOptionalInstance ) {
- ObjectNotFoundException.throwIfNull( entity, event.getEntityId(), event.getEntityClassName() );
+ if ( entity == null ) {
+ event.getSession().getFactory().getEntityNotFoundDelegate().handleEntityNotFound( event.getEntityClassName(), event.getEntityId() );
+ }
}
if ( isOptionalInstance && entity != event.getInstanceToLoad() ) {
@@ -198,9 +199,13 @@
if ( li.isUnwrap() ) {
return li.getImplementation();
}
- // return existing or narrowed proxy
- Object impl = options.isAllowProxyCreation() ?
- null : load(event, persister, keyToLoad, options);
+ Object impl = null;
+ if ( !options.isAllowProxyCreation() ) {
+ impl = load( event, persister, keyToLoad, options );
+ if ( impl == null ) {
+ event.getSession().getFactory().getEntityNotFoundDelegate().handleEntityNotFound( persister.getEntityName(), keyToLoad.getIdentifier());
+ }
+ }
return persistenceContext.narrowProxy( proxy, persister, keyToLoad, impl );
}
Modified: trunk/Hibernate3/src/org/hibernate/impl/SessionFactoryImpl.java
===================================================================
--- trunk/Hibernate3/src/org/hibernate/impl/SessionFactoryImpl.java 2006-05-02 15:11:17 UTC (rev 9854)
+++ trunk/Hibernate3/src/org/hibernate/impl/SessionFactoryImpl.java 2006-05-02 18:55:45 UTC (rev 9855)
@@ -33,6 +33,8 @@
import org.hibernate.QueryException;
import org.hibernate.SessionFactory;
import org.hibernate.StatelessSession;
+import org.hibernate.ObjectNotFoundException;
+import org.hibernate.proxy.EntityNotFoundDelegate;
import org.hibernate.context.CurrentSessionContext;
import org.hibernate.context.ThreadLocalSessionContext;
import org.hibernate.context.JTASessionContext;
@@ -137,6 +139,7 @@
private final transient StatisticsImpl statistics = new StatisticsImpl(this);
private final transient EventListeners eventListeners;
private final transient CurrentSessionContext currentSessionContext;
+ private final transient EntityNotFoundDelegate entityNotFoundDelegate;
private final QueryPlanCache queryPlanCache = new QueryPlanCache( this );
@@ -167,9 +170,11 @@
log.debug("Session factory constructed with filter configurations : " + filters);
}
- if ( log.isDebugEnabled() ) log.debug(
- "instantiating session factory with properties: " + properties
- );
+ if ( log.isDebugEnabled() ) {
+ log.debug(
+ "instantiating session factory with properties: " + properties
+ );
+ }
// Caches
settings.getCacheProvider().start( properties );
@@ -236,7 +241,9 @@
settings,
properties
);
- if (cache!=null) allCacheRegions.put( cache.getRegionName(), cache.getCache() );
+ if ( cache != null ) {
+ allCacheRegions.put( cache.getRegionName(), cache.getCache() );
+ }
CollectionPersister persister = PersisterFactory.createCollectionPersister(cfg, model, cache, this);
collectionPersisters.put( model.getRole(), persister.getCollectionMetadata() );
Type indexType = persister.getIndexType();
@@ -297,10 +304,18 @@
log.debug("instantiated session factory");
- if ( settings.isAutoCreateSchema() ) new SchemaExport(cfg, settings).create(false, true);
- if ( settings.isAutoUpdateSchema() ) new SchemaUpdate(cfg, settings).execute(false, true);
- if ( settings.isAutoValidateSchema() ) new SchemaValidator(cfg, settings).validate();
- if ( settings.isAutoDropSchema() ) schemaExport = new SchemaExport(cfg, settings);
+ if ( settings.isAutoCreateSchema() ) {
+ new SchemaExport( cfg, settings ).create( false, true );
+ }
+ if ( settings.isAutoUpdateSchema() ) {
+ new SchemaUpdate( cfg, settings ).execute( false, true );
+ }
+ if ( settings.isAutoValidateSchema() ) {
+ new SchemaValidator( cfg, settings ).validate();
+ }
+ if ( settings.isAutoDropSchema() ) {
+ schemaExport = new SchemaExport( cfg, settings );
+ }
if ( settings.getTransactionManagerLookup()!=null ) {
log.debug("obtaining JTA TransactionManager");
@@ -338,7 +353,9 @@
String queryName = ( String ) iterator.next();
HibernateException e = ( HibernateException ) errors.get( queryName );
failingQueries.append( queryName );
- if ( iterator.hasNext() ) failingQueries.append(", ");
+ if ( iterator.hasNext() ) {
+ failingQueries.append( ", " );
+ }
log.error( "Error in named query: " + queryName, e );
}
throw new HibernateException( failingQueries.toString() );
@@ -346,6 +363,17 @@
//stats
getStatistics().setStatisticsEnabled( settings.isStatisticsEnabled() );
+
+ // EntityNotFoundDelegate
+ EntityNotFoundDelegate entityNotFoundDelegate = cfg.getEntityNotFoundDelegate();
+ if ( entityNotFoundDelegate == null ) {
+ entityNotFoundDelegate = new EntityNotFoundDelegate() {
+ public void handleEntityNotFound(String entityName, Serializable id) {
+ throw new ObjectNotFoundException( id, entityName );
+ }
+ };
+ }
+ this.entityNotFoundDelegate = entityNotFoundDelegate;
}
public QueryPlanCache getQueryPlanCache() {
@@ -665,7 +693,9 @@
String testClassName = testQueryable.getEntityName();
boolean isMappedClass = className.equals(testClassName);
if ( testQueryable.isExplicitPolymorphism() ) {
- if (isMappedClass) return new String[] { className }; //NOTE EARLY EXIT
+ if ( isMappedClass ) {
+ return new String[] {className}; //NOTE EARLY EXIT
+ }
}
else {
if (isMappedClass) {
@@ -682,7 +712,9 @@
else {
assignableSuperclass = false;
}
- if (!assignableSuperclass) results.add(testClassName);
+ if ( !assignableSuperclass ) {
+ results.add( testClassName );
+ }
}
}
}
@@ -738,13 +770,17 @@
Iterator iter = entityPersisters.values().iterator();
while ( iter.hasNext() ) {
EntityPersister p = (EntityPersister) iter.next();
- if ( p.hasCache() ) p.getCache().destroy();
+ if ( p.hasCache() ) {
+ p.getCache().destroy();
+ }
}
iter = collectionPersisters.values().iterator();
while ( iter.hasNext() ) {
CollectionPersister p = (CollectionPersister) iter.next();
- if ( p.hasCache() ) p.getCache().destroy();
+ if ( p.hasCache() ) {
+ p.getCache().destroy();
+ }
}
if ( settings.isQueryCacheEnabled() ) {
@@ -767,7 +803,9 @@
SessionFactoryObjectFactory.removeInstance(uuid, name, properties);
}
- if ( settings.isAutoDropSchema() ) schemaExport.drop(false, true);
+ if ( settings.isAutoDropSchema() ) {
+ schemaExport.drop( false, true );
+ }
}
@@ -827,7 +865,9 @@
public void evictCollection(String roleName) throws HibernateException {
CollectionPersister p = getCollectionPersister(roleName);
if ( p.hasCache() ) {
- if ( log.isDebugEnabled() ) log.debug( "evicting second-level cache: " + p.getRole() );
+ if ( log.isDebugEnabled() ) {
+ log.debug( "evicting second-level cache: " + p.getRole() );
+ }
p.getCache().clear();
}
}
@@ -908,7 +948,9 @@
synchronized (allCacheRegions) {
if ( settings.isQueryCacheEnabled() ) {
QueryCache currentQueryCache = (QueryCache) queryCaches.get(cacheRegion);
- if (currentQueryCache!=null) currentQueryCache.clear();
+ if ( currentQueryCache != null ) {
+ currentQueryCache.clear();
+ }
}
}
}
@@ -972,6 +1014,10 @@
return eventListeners;
}
+ public EntityNotFoundDelegate getEntityNotFoundDelegate() {
+ return entityNotFoundDelegate;
+ }
+
/**
* Custom serialization hook used during Session serialization.
*
Modified: trunk/Hibernate3/src/org/hibernate/impl/SessionImpl.java
===================================================================
--- trunk/Hibernate3/src/org/hibernate/impl/SessionImpl.java 2006-05-02 15:11:17 UTC (rev 9854)
+++ trunk/Hibernate3/src/org/hibernate/impl/SessionImpl.java 2006-05-02 18:55:45 UTC (rev 9855)
@@ -783,7 +783,9 @@
boolean success = false;
try {
fireLoad( event, LoadEventListener.LOAD );
- ObjectNotFoundException.throwIfNull( event.getResult(), id, entityName);
+ if ( event.getResult() == null ) {
+ getFactory().getEntityNotFoundDelegate().handleEntityNotFound( entityName, id );
+ }
success = true;
return event.getResult();
}
Modified: trunk/Hibernate3/src/org/hibernate/proxy/AbstractLazyInitializer.java
===================================================================
--- trunk/Hibernate3/src/org/hibernate/proxy/AbstractLazyInitializer.java 2006-05-02 15:11:17 UTC (rev 9854)
+++ trunk/Hibernate3/src/org/hibernate/proxy/AbstractLazyInitializer.java 2006-05-02 18:55:45 UTC (rev 9855)
@@ -65,14 +65,18 @@
else {
target = session.immediateLoad(entityName, id);
initialized = true;
- if (!unwrap) {
- ObjectNotFoundException.throwIfNull(target, id, entityName); //should it be UnresolvableObject?
- }
+ checkTargetState();
}
}
else {
- if ( !unwrap ) {
- ObjectNotFoundException.throwIfNull(target, id, entityName);
+ checkTargetState();
+ }
+ }
+
+ private void checkTargetState() {
+ if ( !unwrap ) {
+ if ( target == null ) {
+ getSession().getFactory().getEntityNotFoundDelegate().handleEntityNotFound( entityName, id );
}
}
}
Added: trunk/Hibernate3/src/org/hibernate/proxy/EntityNotFoundDelegate.java
===================================================================
--- trunk/Hibernate3/src/org/hibernate/proxy/EntityNotFoundDelegate.java 2006-05-02 15:11:17 UTC (rev 9854)
+++ trunk/Hibernate3/src/org/hibernate/proxy/EntityNotFoundDelegate.java 2006-05-02 18:55:45 UTC (rev 9855)
@@ -0,0 +1,12 @@
+package org.hibernate.proxy;
+
+import java.io.Serializable;
+
+/**
+ * Delegate to handle the scenario of an entity not found by a specified id.
+ *
+ * @author Steve Ebersole
+ */
+public interface EntityNotFoundDelegate {
+ public void handleEntityNotFound(String entityName, Serializable id);
+}
Modified: trunk/Hibernate3/test/org/hibernate/test/ejb3/EJB3Suite.java
===================================================================
--- trunk/Hibernate3/test/org/hibernate/test/ejb3/EJB3Suite.java 2006-05-02 15:11:17 UTC (rev 9854)
+++ trunk/Hibernate3/test/org/hibernate/test/ejb3/EJB3Suite.java 2006-05-02 18:55:45 UTC (rev 9855)
@@ -4,6 +4,7 @@
import junit.framework.TestSuite;
import org.hibernate.test.ejb3.lock.EJB3LockTest;
import org.hibernate.test.ejb3.lock.RepeatableReadTest;
+import org.hibernate.test.ejb3.proxy.Ejb3ProxyTest;
/**
* @author Steve Ebersole
@@ -13,6 +14,7 @@
TestSuite suite = new TestSuite( "EJB3-compliance tests");
suite.addTest( EJB3LockTest.suite() );
suite.addTest( RepeatableReadTest.suite() );
+ suite.addTest( Ejb3ProxyTest.suite() );
return suite;
}
}
Added: trunk/Hibernate3/test/org/hibernate/test/ejb3/proxy/Ejb3ProxyTest.java
===================================================================
--- trunk/Hibernate3/test/org/hibernate/test/ejb3/proxy/Ejb3ProxyTest.java 2006-05-02 15:11:17 UTC (rev 9854)
+++ trunk/Hibernate3/test/org/hibernate/test/ejb3/proxy/Ejb3ProxyTest.java 2006-05-02 18:55:45 UTC (rev 9855)
@@ -0,0 +1,141 @@
+package org.hibernate.test.ejb3.proxy;
+
+import org.hibernate.test.TestCase;
+import org.hibernate.test.ejb3.Item;
+import org.hibernate.cfg.Configuration;
+import org.hibernate.proxy.EntityNotFoundDelegate;
+import org.hibernate.Session;
+import org.hibernate.Transaction;
+import org.hibernate.Hibernate;
+
+import java.io.Serializable;
+
+import junit.framework.AssertionFailedError;
+import junit.framework.Test;
+import junit.framework.TestSuite;
+
+/**
+ * Test relation between proxies and get()/load() processing
+ * and make sure the interactions match the ejb3 expectations
+ *
+ * @author Steve Ebersole
+ */
+public class Ejb3ProxyTest extends TestCase {
+ public Ejb3ProxyTest(String name) {
+ super( name );
+ }
+
+ public static Test suite() {
+ return new TestSuite( Ejb3ProxyTest.class );
+ }
+
+ public void testEjb3ProxyUsage() {
+ Session s = openSession();
+ Transaction txn = s.beginTransaction();
+
+ Item item = ( Item ) s.load( Item.class, new Long(-1) );
+ assertFalse( Hibernate.isInitialized( item ) );
+ try {
+ Hibernate.initialize( item );
+ fail( "proxy access did not fail on non-existent proxy" );
+ }
+ catch ( EntityNotFoundException e ) {
+ // expected behavior
+ }
+ catch ( Throwable t ) {
+ fail( "unexpected exception type on non-existent proxy access : " + t );
+ }
+
+ s.clear();
+
+ Item item2 = ( Item ) s.load( Item.class, new Long(-1) );
+ assertFalse( Hibernate.isInitialized( item2 ) );
+ assertFalse( item == item2 );
+ try {
+ item2.getName();
+ fail( "proxy access did not fail on non-existent proxy" );
+ }
+ catch ( EntityNotFoundException e ) {
+ // expected behavior
+ }
+ catch ( Throwable t ) {
+ fail( "unexpected exception type on non-existent proxy access : " + t );
+ }
+
+ txn.commit();
+ s.close();
+ }
+
+ /**
+ * The ejb3 find() method maps to the Hibernate get() method
+ */
+ public void testGetSemantics() {
+ Long nonExistentId = new Long( -1 );
+ Session s = openSession();
+ Transaction txn = s.beginTransaction();
+ Item item = ( Item ) s.get( Item.class, nonExistentId );
+ assertNull( "get() of non-existent entity did not return null", item );
+ txn.commit();
+ s.close();
+
+ s = openSession();
+ txn = s.beginTransaction();
+ // first load() it to generate a proxy...
+ item = ( Item ) s.load( Item.class, nonExistentId );
+ assertFalse( Hibernate.isInitialized( item ) );
+ // then try to get() it to make sure we get an exception
+ try {
+ Item item2 = ( Item ) s.get( Item.class, nonExistentId );
+ fail( "force load did not fail on non-existent entity" );
+ }
+ catch ( EntityNotFoundException e ) {
+ // expected behavior
+ }
+ catch( AssertionFailedError e ) {
+ throw e;
+ }
+ catch ( Throwable t ) {
+ fail( "unexpected exception type on non-existent entity force load : " + t );
+ }
+ txn.commit();
+ s.close();
+ }
+
+ protected void configure(Configuration cfg) {
+ super.configure( cfg );
+ cfg.setEntityNotFoundDelegate( new Ejb3EntityNotFoundDelegate() );
+ }
+
+ protected String[] getMappings() {
+ return new String[] { "ejb3/Item.hbm.xml", "ejb3/Part.hbm.xml" };
+ }
+
+ private static class Ejb3EntityNotFoundDelegate implements EntityNotFoundDelegate {
+ public void handleEntityNotFound(String entityName, Serializable id) {
+ throw new EntityNotFoundException( entityName, id );
+ }
+ }
+
+ private static class EntityNotFoundException extends RuntimeException {
+ private final String entityName;
+ private final Serializable id;
+
+ public EntityNotFoundException(String entityName, Serializable id) {
+ this( "unable to locate specified entity", entityName, id );
+ }
+
+ public EntityNotFoundException(String message, String entityName, Serializable id) {
+ super( message );
+ this.entityName = entityName;
+ this.id = id;
+ }
+
+ public String getEntityName() {
+ return entityName;
+ }
+
+ public Serializable getId() {
+ return id;
+ }
+ }
+}
Modified: trunk/Hibernate3/test/org/hibernate/test/proxy/ProxyTest.java
===================================================================
--- trunk/Hibernate3/test/org/hibernate/test/proxy/ProxyTest.java 2006-05-02 15:11:17 UTC (rev 9854)
+++ trunk/Hibernate3/test/org/hibernate/test/proxy/ProxyTest.java 2006-05-02 18:55:45 UTC (rev 9855)
@@ -237,7 +237,6 @@
s.close();
}
-
protected String[] getMappings() {
return new String[] { "proxy/DataPoint.hbm.xml" };
}
|