From: <hib...@li...> - 2006-08-04 20:33:54
|
Author: ste...@jb... Date: 2006-08-04 16:33:48 -0400 (Fri, 04 Aug 2006) New Revision: 10225 Added: branches/Branch_3_2/Hibernate3/test/org/hibernate/test/proxy/Info.java Modified: branches/Branch_3_2/Hibernate3/src/org/hibernate/event/def/DefaultEvictEventListener.java branches/Branch_3_2/Hibernate3/test/org/hibernate/test/proxy/Container.java branches/Branch_3_2/Hibernate3/test/org/hibernate/test/proxy/DataPoint.hbm.xml branches/Branch_3_2/Hibernate3/test/org/hibernate/test/proxy/DataPoint.java branches/Branch_3_2/Hibernate3/test/org/hibernate/test/proxy/ProxyTest.java Log: HHH-1954 : proxies and eviction Modified: branches/Branch_3_2/Hibernate3/src/org/hibernate/event/def/DefaultEvictEventListener.java =================================================================== --- branches/Branch_3_2/Hibernate3/src/org/hibernate/event/def/DefaultEvictEventListener.java 2006-08-04 20:29:45 UTC (rev 10224) +++ branches/Branch_3_2/Hibernate3/src/org/hibernate/event/def/DefaultEvictEventListener.java 2006-08-04 20:33:48 UTC (rev 10225) @@ -40,20 +40,17 @@ public void onEvict(EvictEvent event) throws HibernateException { EventSource source = event.getSession(); final Object object = event.getObject(); - final PersistenceContext persistenceContext = source.getPersistenceContext(); + if ( object instanceof HibernateProxy ) { - LazyInitializer li = ( (HibernateProxy) object ).getHibernateLazyInitializer(); Serializable id = li.getIdentifier(); EntityPersister persister = source.getFactory().getEntityPersister( li.getEntityName() ); - if (id==null) { + if ( id == null ) { throw new IllegalArgumentException("null identifier"); } - EntityKey key = new EntityKey( id, persister, source.getEntityMode() ); - persistenceContext.removeProxy(key); - + persistenceContext.removeProxy( key ); if ( !li.isUninitialized() ) { final Object entity = persistenceContext.removeEntity(key); if ( entity != null ) { @@ -61,14 +58,13 @@ doEvict( entity, key, e.getPersister(), event.getSession() ); } } - + li.setSession( null ); } else { - - EntityEntry e = persistenceContext.removeEntry(object); + EntityEntry e = persistenceContext.removeEntry( object ); if ( e != null ) { EntityKey key = new EntityKey( e.getId(), e.getPersister(), source.getEntityMode() ); - persistenceContext.removeEntity(key); + persistenceContext.removeEntity( key ); doEvict( object, key, e.getPersister(), source ); } @@ -79,23 +75,18 @@ final Object object, final EntityKey key, final EntityPersister persister, - final EventSource session) - throws HibernateException { + final EventSource session) throws HibernateException { if ( log.isTraceEnabled() ) { log.trace( "evicting " + MessageHelper.infoString(persister) ); } // remove all collections for the entity from the session-level cache - if ( persister.hasCollections() ) new EvictVisitor( session ).process( object, persister ); - - // remove any snapshot, not really for memory management purposes, but - // rather because it might now be stale, and there is no longer any - // EntityEntry to take precedence - // This is now handled by removeEntity() - //session.getPersistenceContext().removeDatabaseSnapshot(key); + if ( persister.hasCollections() ) { + new EvictVisitor( session ).process( object, persister ); + } - new Cascade(CascadingAction.EVICT, Cascade.AFTER_EVICT, session) - .cascade(persister, object); + new Cascade( CascadingAction.EVICT, Cascade.AFTER_EVICT, session ) + .cascade( persister, object ); } } Modified: branches/Branch_3_2/Hibernate3/test/org/hibernate/test/proxy/Container.java =================================================================== --- branches/Branch_3_2/Hibernate3/test/org/hibernate/test/proxy/Container.java 2006-08-04 20:29:45 UTC (rev 10224) +++ branches/Branch_3_2/Hibernate3/test/org/hibernate/test/proxy/Container.java 2006-08-04 20:33:48 UTC (rev 10225) @@ -11,6 +11,7 @@ private Long id; private String name; private Owner owner; + private Info info; private Set dataPoints = new HashSet(); public Container() { @@ -44,6 +45,14 @@ this.owner = owner; } + public Info getInfo() { + return info; + } + + public void setInfo(Info info) { + this.info = info; + } + public Set getDataPoints() { return dataPoints; } Modified: branches/Branch_3_2/Hibernate3/test/org/hibernate/test/proxy/DataPoint.hbm.xml =================================================================== --- branches/Branch_3_2/Hibernate3/test/org/hibernate/test/proxy/DataPoint.hbm.xml 2006-08-04 20:29:45 UTC (rev 10224) +++ branches/Branch_3_2/Hibernate3/test/org/hibernate/test/proxy/DataPoint.hbm.xml 2006-08-04 20:33:48 UTC (rev 10225) @@ -26,12 +26,20 @@ <property name="name" unique="true"/> </class> + <class name="Info"> + <id name="id"> + <generator class="increment"/> + </id> + <property name="details"/> + </class> + <class name="Container"> <id name="id"> <generator class="increment"/> </id> <property name="name"/> <many-to-one name="owner" class="Owner" column="owner_name" property-ref="name" cascade="all"/> + <many-to-one name="info" class="Info" column="info_id" cascade="all"/> <set name="dataPoints" lazy="true" inverse="false" cascade="all"> <key column="c_id"/> <one-to-many class="DataPoint"/> Modified: branches/Branch_3_2/Hibernate3/test/org/hibernate/test/proxy/DataPoint.java =================================================================== --- branches/Branch_3_2/Hibernate3/test/org/hibernate/test/proxy/DataPoint.java 2006-08-04 20:29:45 UTC (rev 10224) +++ branches/Branch_3_2/Hibernate3/test/org/hibernate/test/proxy/DataPoint.java 2006-08-04 20:33:48 UTC (rev 10225) @@ -12,6 +12,16 @@ private BigDecimal x; private BigDecimal y; private String description; + + public DataPoint() { + } + + public DataPoint(BigDecimal x, BigDecimal y, String description) { + this.x = x; + this.y = y; + this.description = description; + } + /** * @return Returns the description. */ Added: branches/Branch_3_2/Hibernate3/test/org/hibernate/test/proxy/Info.java =================================================================== --- branches/Branch_3_2/Hibernate3/test/org/hibernate/test/proxy/Info.java 2006-08-04 20:29:45 UTC (rev 10224) +++ branches/Branch_3_2/Hibernate3/test/org/hibernate/test/proxy/Info.java 2006-08-04 20:33:48 UTC (rev 10225) @@ -0,0 +1,34 @@ +package org.hibernate.test.proxy; + +/** + * todo: describe Info + * + * @author Steve Ebersole + */ +public class Info { + private Long id; + private String details; + + public Info() { + } + + public Info(String details) { + this.details = details; + } + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public String getDetails() { + return details; + } + + public void setDetails(String details) { + this.details = details; + } +} Modified: branches/Branch_3_2/Hibernate3/test/org/hibernate/test/proxy/ProxyTest.java =================================================================== --- branches/Branch_3_2/Hibernate3/test/org/hibernate/test/proxy/ProxyTest.java 2006-08-04 20:29:45 UTC (rev 10224) +++ branches/Branch_3_2/Hibernate3/test/org/hibernate/test/proxy/ProxyTest.java 2006-08-04 20:33:48 UTC (rev 10225) @@ -13,6 +13,7 @@ import org.hibernate.Transaction; import org.hibernate.FlushMode; import org.hibernate.ObjectNotFoundException; +import org.hibernate.LazyInitializationException; import org.hibernate.cfg.Configuration; import org.hibernate.cfg.Environment; import org.hibernate.impl.SessionImpl; @@ -237,6 +238,51 @@ s.close(); } + public void testProxyEviction() { + Session s = openSession(); + Transaction t = s.beginTransaction(); + Container container = new Container( "container" ); + container.setOwner( new Owner( "owner" ) ); + container.setInfo( new Info( "blah blah blah" ) ); + container.getDataPoints().add( new DataPoint( new BigDecimal( 1 ), new BigDecimal( 1 ), "first data point" ) ); + container.getDataPoints().add( new DataPoint( new BigDecimal( 2 ), new BigDecimal( 2 ), "second data point" ) ); + s.save( container ); + t.commit(); + s.close(); + + s = openSession(); + t = s.beginTransaction(); + Container c = ( Container ) s.load( Container.class, container.getId() ); + assertFalse( Hibernate.isInitialized( c ) ); + s.evict( c ); + try { + c.getName(); + fail( "expecting LazyInitializationException" ); + } + catch( LazyInitializationException e ) { + // expected result + } + + c = ( Container ) s.load( Container.class, container.getId() ); + assertFalse( Hibernate.isInitialized( c ) ); + Info i = c.getInfo(); + assertTrue( Hibernate.isInitialized( c ) ); + assertFalse( Hibernate.isInitialized( i ) ); + s.evict( c ); + try { + i.getDetails(); + fail( "expecting LazyInitializationException" ); + } + catch( LazyInitializationException e ) { + // expected result + } + + s.delete( c ); + + t.commit(); + s.close(); + } + protected String[] getMappings() { return new String[] { "proxy/DataPoint.hbm.xml" }; } |