[hibernate-commits] Hibernate SVN: r14312 - in core/branches/Branch_3_2: src/org/hibernate/engine and 3 other directories.
hibernate-commits at lists.jboss.org
hibernate-commits at lists.jboss.org
Tue Feb 5 18:55:35 EST 2008
Author: gbadner
Date: 2008-02-05 18:55:35 -0500 (Tue, 05 Feb 2008)
New Revision: 14312
Modified:
core/branches/Branch_3_2/src/org/hibernate/action/CollectionRemoveAction.java
core/branches/Branch_3_2/src/org/hibernate/engine/PersistenceContext.java
core/branches/Branch_3_2/src/org/hibernate/engine/StatefulPersistenceContext.java
core/branches/Branch_3_2/src/org/hibernate/event/AbstractCollectionEvent.java
core/branches/Branch_3_2/src/org/hibernate/event/InitializeCollectionEvent.java
core/branches/Branch_3_2/src/org/hibernate/event/PostCollectionRecreateEvent.java
core/branches/Branch_3_2/src/org/hibernate/event/PostCollectionRemoveEvent.java
core/branches/Branch_3_2/src/org/hibernate/event/PostCollectionUpdateEvent.java
core/branches/Branch_3_2/src/org/hibernate/event/PreCollectionRecreateEvent.java
core/branches/Branch_3_2/src/org/hibernate/event/PreCollectionRemoveEvent.java
core/branches/Branch_3_2/src/org/hibernate/event/PreCollectionUpdateEvent.java
core/branches/Branch_3_2/src/org/hibernate/type/CollectionType.java
core/branches/Branch_3_2/test/org/hibernate/test/event/collection/AbstractCollectionEventTest.java
core/branches/Branch_3_2/test/org/hibernate/test/event/collection/BrokenCollectionEventTest.java
Log:
HHH-2616 : added owner ID getter to collection events; added partial support for property-refs; added Javadoc
Modified: core/branches/Branch_3_2/src/org/hibernate/action/CollectionRemoveAction.java
===================================================================
--- core/branches/Branch_3_2/src/org/hibernate/action/CollectionRemoveAction.java 2008-02-04 22:32:36 UTC (rev 14311)
+++ core/branches/Branch_3_2/src/org/hibernate/action/CollectionRemoveAction.java 2008-02-05 23:55:35 UTC (rev 14312)
@@ -1,20 +1,20 @@
//$Id$
package org.hibernate.action;
+import java.io.Serializable;
+
+import org.hibernate.AssertionFailure;
import org.hibernate.HibernateException;
-import org.hibernate.AssertionFailure;
+import org.hibernate.cache.CacheException;
+import org.hibernate.collection.PersistentCollection;
+import org.hibernate.engine.SessionImplementor;
+import org.hibernate.event.EventSource;
import org.hibernate.event.PostCollectionRemoveEvent;
+import org.hibernate.event.PostCollectionRemoveEventListener;
import org.hibernate.event.PreCollectionRemoveEvent;
import org.hibernate.event.PreCollectionRemoveEventListener;
-import org.hibernate.event.EventSource;
-import org.hibernate.event.PostCollectionRemoveEventListener;
-import org.hibernate.cache.CacheException;
-import org.hibernate.collection.PersistentCollection;
-import org.hibernate.engine.SessionImplementor;
import org.hibernate.persister.collection.CollectionPersister;
-import java.io.Serializable;
-
public final class CollectionRemoveAction extends CollectionAction {
private boolean emptySnapshot;
@@ -45,7 +45,7 @@
// the loaded owner will be set to null after the collection is removed,
// so capture its value as the affected owner so it is accessible to
// both pre- and post- events
- this.affectedOwner = session.getPersistenceContext().getLoadedCollectionOwner( collection );
+ this.affectedOwner = session.getPersistenceContext().getLoadedCollectionOwnerOrNull( collection );
}
/**
@@ -68,7 +68,7 @@
final SessionImplementor session)
throws CacheException {
super( persister, null, id, session );
- if (affectedOwner == null) { throw new AssertionFailure("affectedOwner == null"); };
+ if (affectedOwner == null) { throw new AssertionFailure("affectedOwner == null"); }
this.emptySnapshot = emptySnapshot;
this.affectedOwner = affectedOwner;
}
Modified: core/branches/Branch_3_2/src/org/hibernate/engine/PersistenceContext.java
===================================================================
--- core/branches/Branch_3_2/src/org/hibernate/engine/PersistenceContext.java 2008-02-04 22:32:36 UTC (rev 14311)
+++ core/branches/Branch_3_2/src/org/hibernate/engine/PersistenceContext.java 2008-02-05 23:55:35 UTC (rev 14312)
@@ -252,10 +252,22 @@
/**
* Get the entity that owned this persistent collection when it was loaded
+ *
+ * @param collection The persistent collection
+ * @return the owner if its entity ID is available from the collection's loaded key
+ * and the owner entity is in the persistence context; otherwise, returns null
*/
- Object getLoadedCollectionOwner(PersistentCollection collection);
+ Object getLoadedCollectionOwnerOrNull(PersistentCollection collection);
/**
+ * Get the ID for the entity that owned this persistent collection when it was loaded
+ *
+ * @param collection The persistent collection
+ * @return the owner ID if available from the collection's loaded key; otherwise, returns null
+ */
+ public Serializable getLoadedCollectionOwnerIdOrNull(PersistentCollection collection);
+
+ /**
* add a collection we just loaded up (still needs initializing)
*/
public void addUninitializedCollection(CollectionPersister persister,
Modified: core/branches/Branch_3_2/src/org/hibernate/engine/StatefulPersistenceContext.java
===================================================================
--- core/branches/Branch_3_2/src/org/hibernate/engine/StatefulPersistenceContext.java 2008-02-04 22:32:36 UTC (rev 14311)
+++ core/branches/Branch_3_2/src/org/hibernate/engine/StatefulPersistenceContext.java 2008-02-05 23:55:35 UTC (rev 14312)
@@ -680,13 +680,49 @@
/**
* Get the entity that owned this persistent collection when it was loaded
+ *
+ * @param collection The persistent collection
+ * @return the owner, if its entity ID is available from the collection's loaded key
+ * and the owner entity is in the persistence context; otherwise, returns null
*/
- public Object getLoadedCollectionOwner(PersistentCollection collection) {
- CollectionEntry ce = getCollectionEntry(collection);
- if ( ce.getLoadedKey() == null || ce.getLoadedPersister() == null ) {
+ public Object getLoadedCollectionOwnerOrNull(PersistentCollection collection) {
+ CollectionEntry ce = getCollectionEntry( collection );
+ if ( ce.getLoadedPersister() == null ) {
+ return null; // early exit...
+ }
+ Object loadedOwner = null;
+ // TODO: an alternative is to check if the owner has changed; if it hasn't then
+ // return collection.getOwner()
+ Serializable entityId = getLoadedCollectionOwnerIdOrNull( ce );
+ if ( entityId != null ) {
+ loadedOwner = getCollectionOwner( entityId, ce.getLoadedPersister() );
+ }
+ return loadedOwner;
+ }
+
+ /**
+ * Get the ID for the entity that owned this persistent collection when it was loaded
+ *
+ * @param collection The persistent collection
+ * @return the owner ID if available from the collection's loaded key; otherwise, returns null
+ */
+ public Serializable getLoadedCollectionOwnerIdOrNull(PersistentCollection collection) {
+ return getLoadedCollectionOwnerIdOrNull( getCollectionEntry( collection ) );
+ }
+
+ /**
+ * Get the ID for the entity that owned this persistent collection when it was loaded
+ *
+ * @param ce The collection entry
+ * @return the owner ID if available from the collection's loaded key; otherwise, returns null
+ */
+ private Serializable getLoadedCollectionOwnerIdOrNull(CollectionEntry ce) {
+ if ( ce == null || ce.getLoadedKey() == null || ce.getLoadedPersister() == null ) {
return null;
}
- return getCollectionOwner(ce.getLoadedKey(), ce.getLoadedPersister());
+ // TODO: an alternative is to check if the owner has changed; if it hasn't then
+ // get the ID from collection.getOwner()
+ return ce.getLoadedPersister().getCollectionType().getIdOfOwnerOrNull( ce.getLoadedKey(), session );
}
/**
Modified: core/branches/Branch_3_2/src/org/hibernate/event/AbstractCollectionEvent.java
===================================================================
--- core/branches/Branch_3_2/src/org/hibernate/event/AbstractCollectionEvent.java 2008-02-04 22:32:36 UTC (rev 14311)
+++ core/branches/Branch_3_2/src/org/hibernate/event/AbstractCollectionEvent.java 2008-02-05 23:55:35 UTC (rev 14312)
@@ -4,7 +4,7 @@
import java.io.Serializable;
import org.hibernate.collection.PersistentCollection;
-import org.hibernate.engine.SessionImplementor;
+import org.hibernate.engine.EntityEntry;
/**
* Defines a base class for events involving collections.
@@ -15,22 +15,64 @@
private final PersistentCollection collection;
private final Object affectedOwner;
+ private final Serializable affectedOwnerId;
- public AbstractCollectionEvent(PersistentCollection collection, EventSource source, Object affectedOwner) {
+ /**
+ * Constructs an AbstractCollectionEvent object.
+ *
+ * @param collection - the collection
+ * @param source - the Session source
+ * @param affectedOwner - the owner that is affected by this event;
+ * can be null if unavailable
+ * @param affectedOwnerId - the ID for the owner that is affected
+ * by this event; can be null if unavailable
+ * that is affected by this event; can be null if unavailable
+ */
+ public AbstractCollectionEvent(PersistentCollection collection,
+ EventSource source,
+ Object affectedOwner,
+ Serializable affectedOwnerId ) {
super(source);
this.collection = collection;
this.affectedOwner = affectedOwner;
+ this.affectedOwnerId = affectedOwnerId;
}
- protected static Object getLoadedOwner( PersistentCollection collection, EventSource source ) {
- return ( ( SessionImplementor ) source ).getPersistenceContext().getLoadedCollectionOwner( collection );
+ protected static Object getLoadedOwnerOrNull( PersistentCollection collection, EventSource source ) {
+ return source.getPersistenceContext().getLoadedCollectionOwnerOrNull( collection );
}
+ protected static Serializable getLoadedOwnerIdOrNull( PersistentCollection collection, EventSource source ) {
+ return source.getPersistenceContext().getLoadedCollectionOwnerIdOrNull( collection );
+ }
+
+ protected static Serializable getOwnerIdOrNull( Object owner, EventSource source ) {
+ EntityEntry ownerEntry = source.getPersistenceContext().getEntry( owner );
+ return ( ownerEntry == null ? null : ownerEntry.getId() );
+ }
+
public PersistentCollection getCollection() {
return collection;
}
- public Object getAffectedOwner() {
+ /**
+ * Get the collection owner entity that is affected by this event.
+ *
+ * @return the affected owner; returns null if the entity is not in the persistence context
+ * (e.g., because the collection from a detached entity was moved to a new owner)
+ */
+ public Object getAffectedOwnerOrNull() {
return affectedOwner;
}
+
+ /**
+ * Get the ID for the collection owner entity that is affected by this event.
+ *
+ * @return the affected owner ID; returns null if the ID cannot be obtained
+ * from the collection's loaded key (e.g., a property-ref is used for the
+ * collection and does not include the entity's ID)
+ */
+ public Serializable getAffectedOwnerIdOrNull() {
+ return affectedOwnerId;
+ }
}
Modified: core/branches/Branch_3_2/src/org/hibernate/event/InitializeCollectionEvent.java
===================================================================
--- core/branches/Branch_3_2/src/org/hibernate/event/InitializeCollectionEvent.java 2008-02-04 22:32:36 UTC (rev 14311)
+++ core/branches/Branch_3_2/src/org/hibernate/event/InitializeCollectionEvent.java 2008-02-05 23:55:35 UTC (rev 14312)
@@ -12,6 +12,8 @@
public class InitializeCollectionEvent extends AbstractCollectionEvent {
public InitializeCollectionEvent(PersistentCollection collection, EventSource source) {
- super(collection, source, getLoadedOwner( collection, source ) );
+ super( collection, source,
+ getLoadedOwnerOrNull( collection, source ),
+ getLoadedOwnerIdOrNull( collection, source ) );
}
}
Modified: core/branches/Branch_3_2/src/org/hibernate/event/PostCollectionRecreateEvent.java
===================================================================
--- core/branches/Branch_3_2/src/org/hibernate/event/PostCollectionRecreateEvent.java 2008-02-04 22:32:36 UTC (rev 14311)
+++ core/branches/Branch_3_2/src/org/hibernate/event/PostCollectionRecreateEvent.java 2008-02-05 23:55:35 UTC (rev 14312)
@@ -11,6 +11,8 @@
public class PostCollectionRecreateEvent extends AbstractCollectionEvent {
public PostCollectionRecreateEvent(PersistentCollection collection, EventSource source) {
- super(collection, source, collection.getOwner());
+ super( collection, source,
+ collection.getOwner(),
+ getOwnerIdOrNull( collection.getOwner(), source ) );
}
}
Modified: core/branches/Branch_3_2/src/org/hibernate/event/PostCollectionRemoveEvent.java
===================================================================
--- core/branches/Branch_3_2/src/org/hibernate/event/PostCollectionRemoveEvent.java 2008-02-04 22:32:36 UTC (rev 14311)
+++ core/branches/Branch_3_2/src/org/hibernate/event/PostCollectionRemoveEvent.java 2008-02-05 23:55:35 UTC (rev 14312)
@@ -11,6 +11,8 @@
public class PostCollectionRemoveEvent extends AbstractCollectionEvent {
public PostCollectionRemoveEvent(PersistentCollection collection, Object loadedOwner, EventSource source) {
- super(collection, source, loadedOwner);
+ super( collection, source,
+ loadedOwner,
+ getOwnerIdOrNull( loadedOwner, source ) );
}
}
Modified: core/branches/Branch_3_2/src/org/hibernate/event/PostCollectionUpdateEvent.java
===================================================================
--- core/branches/Branch_3_2/src/org/hibernate/event/PostCollectionUpdateEvent.java 2008-02-04 22:32:36 UTC (rev 14311)
+++ core/branches/Branch_3_2/src/org/hibernate/event/PostCollectionUpdateEvent.java 2008-02-05 23:55:35 UTC (rev 14312)
@@ -11,6 +11,8 @@
public class PostCollectionUpdateEvent extends AbstractCollectionEvent {
public PostCollectionUpdateEvent(PersistentCollection collection, EventSource source) {
- super(collection, source, getLoadedOwner( collection, source ));
+ super( collection, source,
+ getLoadedOwnerOrNull( collection, source ),
+ getLoadedOwnerIdOrNull( collection, source ) );
}
}
Modified: core/branches/Branch_3_2/src/org/hibernate/event/PreCollectionRecreateEvent.java
===================================================================
--- core/branches/Branch_3_2/src/org/hibernate/event/PreCollectionRecreateEvent.java 2008-02-04 22:32:36 UTC (rev 14311)
+++ core/branches/Branch_3_2/src/org/hibernate/event/PreCollectionRecreateEvent.java 2008-02-05 23:55:35 UTC (rev 14312)
@@ -11,6 +11,8 @@
public class PreCollectionRecreateEvent extends AbstractCollectionEvent {
public PreCollectionRecreateEvent(PersistentCollection collection, EventSource source) {
- super(collection, source, collection.getOwner());
+ super( collection, source,
+ collection.getOwner(),
+ getOwnerIdOrNull( collection.getOwner(), source ) );
}
}
Modified: core/branches/Branch_3_2/src/org/hibernate/event/PreCollectionRemoveEvent.java
===================================================================
--- core/branches/Branch_3_2/src/org/hibernate/event/PreCollectionRemoveEvent.java 2008-02-04 22:32:36 UTC (rev 14311)
+++ core/branches/Branch_3_2/src/org/hibernate/event/PreCollectionRemoveEvent.java 2008-02-05 23:55:35 UTC (rev 14312)
@@ -11,6 +11,8 @@
public class PreCollectionRemoveEvent extends AbstractCollectionEvent {
public PreCollectionRemoveEvent(PersistentCollection collection, Object loadedOwner, EventSource source) {
- super(collection, source, loadedOwner);
+ super( collection, source,
+ loadedOwner,
+ getOwnerIdOrNull( loadedOwner, source ) );
}
}
Modified: core/branches/Branch_3_2/src/org/hibernate/event/PreCollectionUpdateEvent.java
===================================================================
--- core/branches/Branch_3_2/src/org/hibernate/event/PreCollectionUpdateEvent.java 2008-02-04 22:32:36 UTC (rev 14311)
+++ core/branches/Branch_3_2/src/org/hibernate/event/PreCollectionUpdateEvent.java 2008-02-05 23:55:35 UTC (rev 14312)
@@ -11,6 +11,8 @@
public class PreCollectionUpdateEvent extends AbstractCollectionEvent {
public PreCollectionUpdateEvent(PersistentCollection collection, EventSource source) {
- super(collection, source, getLoadedOwner( collection, source ));
+ super( collection, source,
+ getLoadedOwnerOrNull( collection, source ),
+ getLoadedOwnerIdOrNull( collection, source ) );
}
}
Modified: core/branches/Branch_3_2/src/org/hibernate/type/CollectionType.java
===================================================================
--- core/branches/Branch_3_2/src/org/hibernate/type/CollectionType.java 2008-02-04 22:32:36 UTC (rev 14311)
+++ core/branches/Branch_3_2/src/org/hibernate/type/CollectionType.java 2008-02-05 23:55:35 UTC (rev 14312)
@@ -13,6 +13,7 @@
import org.dom4j.Element;
import org.dom4j.Node;
+
import org.hibernate.EntityMode;
import org.hibernate.Hibernate;
import org.hibernate.HibernateException;
@@ -26,6 +27,7 @@
import org.hibernate.engine.SessionImplementor;
import org.hibernate.persister.collection.CollectionPersister;
import org.hibernate.persister.collection.QueryableCollection;
+import org.hibernate.persister.entity.EntityPersister;
import org.hibernate.persister.entity.Joinable;
import org.hibernate.proxy.HibernateProxy;
import org.hibernate.proxy.LazyInitializer;
@@ -343,6 +345,7 @@
}
// NOTE VERY HACKISH WORKAROUND!!
+ // TODO: Fix this so it will work for non-POJO entity mode
Type keyType = getPersister( session ).getKeyType();
if ( !keyType.getReturnedClass().isInstance( id ) ) {
id = (Serializable) keyType.semiResolve(
@@ -356,6 +359,37 @@
}
}
+ /**
+ * Get the id value from the owning entity key, usually the same as the key, but might be some
+ * other property, in the case of property-ref
+ *
+ * @param key The collection owner key
+ * @param session The session from which the request is originating.
+ * @return The collection owner's id, if it can be obtained from the key;
+ * otherwise, null is returned
+ */
+ public Serializable getIdOfOwnerOrNull(Serializable key, SessionImplementor session) {
+ Serializable ownerId = null;
+ if ( foreignKeyPropertyName == null ) {
+ ownerId = key;
+ }
+ else {
+ Type keyType = getPersister( session ).getKeyType();
+ EntityPersister ownerPersister = getPersister( session ).getOwnerEntityPersister();
+ // TODO: Fix this so it will work for non-POJO entity mode
+ Class ownerMappedClass = ownerPersister.getMappedClass( session.getEntityMode() );
+ if ( ownerMappedClass.isAssignableFrom( keyType.getReturnedClass() ) &&
+ keyType.getReturnedClass().isInstance( key ) ) {
+ // the key is the owning entity itself, so get the ID from the key
+ ownerId = ownerPersister.getIdentifier( key, session.getEntityMode() );
+ }
+ else {
+ // TODO: check if key contains the owner ID
+ }
+ }
+ return ownerId;
+ }
+
public Object hydrate(ResultSet rs, String[] name, SessionImplementor session, Object owner) {
// can't just return null here, since that would
// cause an owning component to become null
Modified: core/branches/Branch_3_2/test/org/hibernate/test/event/collection/AbstractCollectionEventTest.java
===================================================================
--- core/branches/Branch_3_2/test/org/hibernate/test/event/collection/AbstractCollectionEventTest.java 2008-02-04 22:32:36 UTC (rev 14311)
+++ core/branches/Branch_3_2/test/org/hibernate/test/event/collection/AbstractCollectionEventTest.java 2008-02-05 23:55:35 UTC (rev 14312)
@@ -761,12 +761,12 @@
assertSame( listenerExpected, listeners.getListenersCalled().get( index ) );
assertSame(
ownerExpected,
- ( ( AbstractCollectionEvent ) listeners.getEvents().get( index ) ).getAffectedOwner()
+ ( ( AbstractCollectionEvent ) listeners.getEvents().get( index ) ).getAffectedOwnerOrNull()
);
- //assertSame(
- // ownerExpected.getId(),
- // ( ( AbstractCollectionEvent ) listeners.getEvents().get( index ) ).getAffectedOwnerId()
- //);
+ assertEquals(
+ ownerExpected.getId(),
+ ( ( AbstractCollectionEvent ) listeners.getEvents().get( index ) ).getAffectedOwnerIdOrNull()
+ );
assertSame(
collExpected, ( ( AbstractCollectionEvent ) listeners.getEvents().get( index ) ).getCollection()
);
Modified: core/branches/Branch_3_2/test/org/hibernate/test/event/collection/BrokenCollectionEventTest.java
===================================================================
--- core/branches/Branch_3_2/test/org/hibernate/test/event/collection/BrokenCollectionEventTest.java 2008-02-04 22:32:36 UTC (rev 14311)
+++ core/branches/Branch_3_2/test/org/hibernate/test/event/collection/BrokenCollectionEventTest.java 2008-02-05 23:55:35 UTC (rev 14312)
@@ -311,12 +311,12 @@
assertSame( listenerExpected, listeners.getListenersCalled().get( index ) );
assertSame(
ownerExpected,
- ( ( AbstractCollectionEvent ) listeners.getEvents().get( index ) ).getAffectedOwner()
+ ( ( AbstractCollectionEvent ) listeners.getEvents().get( index ) ).getAffectedOwnerOrNull()
);
- //assertSame(
- // ownerExpected.getId(),
- // ( ( AbstractCollectionEvent ) listeners.getEvents().get( index ) ).getAffectedOwnerId()
- //);
+ assertEquals(
+ ownerExpected.getId(),
+ ( ( AbstractCollectionEvent ) listeners.getEvents().get( index ) ).getAffectedOwnerIdOrNull()
+ );
assertSame(
collExpected, ( ( AbstractCollectionEvent ) listeners.getEvents().get( index ) ).getCollection()
);
More information about the hibernate-commits
mailing list