HHH-2616 : added getter for owner entitity name to collection events

git-svn-id: https://svn.jboss.org/repos/hibernate/core/branches/Branch_3_2@14313 1b8cb986-b30d-0410-93ca-fae66ebed9b2
This commit is contained in:
Gail Badner 2008-02-06 07:46:52 +00:00
parent 914e233f90
commit 83f06c38ba
16 changed files with 229 additions and 28 deletions

View File

@ -57,7 +57,7 @@ public final class CollectionRecreateAction extends CollectionAction {
.getPreCollectionRecreateEventListeners();
if (preListeners.length > 0) {
PreCollectionRecreateEvent preEvent = new PreCollectionRecreateEvent(
getCollection(), ( EventSource ) getSession() );
getPersister(), getCollection(), ( EventSource ) getSession() );
for ( int i = 0; i < preListeners.length; i++ ) {
preListeners[i].onPreRecreateCollection( preEvent );
}
@ -69,7 +69,7 @@ public final class CollectionRecreateAction extends CollectionAction {
.getPostCollectionRecreateEventListeners();
if (postListeners.length > 0) {
PostCollectionRecreateEvent postEvent = new PostCollectionRecreateEvent(
getCollection(), ( EventSource ) getSession() );
getPersister(), getCollection(), ( EventSource ) getSession() );
for ( int i = 0; i < postListeners.length; i++ ) {
postListeners[i].onPostRecreateCollection( postEvent );
}

View File

@ -110,9 +110,7 @@ public final class CollectionRemoveAction extends CollectionAction {
.getPreCollectionRemoveEventListeners();
if (preListeners.length>0) {
PreCollectionRemoveEvent preEvent = new PreCollectionRemoveEvent(
getCollection(),
affectedOwner,
( EventSource )getSession() );
getPersister(), getCollection(), ( EventSource ) getSession(), affectedOwner );
for ( int i = 0; i < preListeners.length; i++ ) {
preListeners[i].onPreRemoveCollection(preEvent);
}
@ -124,9 +122,7 @@ public final class CollectionRemoveAction extends CollectionAction {
.getPostCollectionRemoveEventListeners();
if (postListeners.length>0) {
PostCollectionRemoveEvent postEvent = new PostCollectionRemoveEvent(
getCollection(),
affectedOwner,
( EventSource )getSession() );
getPersister(), getCollection(), ( EventSource ) getSession(), affectedOwner );
for ( int i = 0; i < postListeners.length; i++ ) {
postListeners[i].onPostRemoveCollection(postEvent);
}

View File

@ -85,7 +85,7 @@ public final class CollectionUpdateAction extends CollectionAction {
.getPreCollectionUpdateEventListeners();
if (preListeners.length > 0) {
PreCollectionUpdateEvent preEvent = new PreCollectionUpdateEvent(
getCollection(), ( EventSource ) getSession() );
getPersister(), getCollection(), ( EventSource ) getSession() );
for ( int i = 0; i < preListeners.length; i++ ) {
preListeners[i].onPreUpdateCollection( preEvent );
}
@ -97,7 +97,7 @@ public final class CollectionUpdateAction extends CollectionAction {
.getPostCollectionUpdateEventListeners();
if (postListeners.length > 0) {
PostCollectionUpdateEvent postEvent = new PostCollectionUpdateEvent(
getCollection(), ( EventSource ) getSession() );
getPersister(), getCollection(), ( EventSource ) getSession() );
for ( int i = 0; i < postListeners.length; i++ ) {
postListeners[i].onPostUpdateCollection( postEvent );
}

View File

@ -4,7 +4,9 @@ package org.hibernate.event;
import java.io.Serializable;
import org.hibernate.collection.PersistentCollection;
import org.hibernate.engine.CollectionEntry;
import org.hibernate.engine.EntityEntry;
import org.hibernate.persister.collection.CollectionPersister;
/**
* Defines a base class for events involving collections.
@ -16,6 +18,7 @@ public abstract class AbstractCollectionEvent extends AbstractEvent {
private final PersistentCollection collection;
private final Object affectedOwner;
private final Serializable affectedOwnerId;
private final String affectedOwnerEntityName;
/**
* Constructs an AbstractCollectionEvent object.
@ -28,14 +31,22 @@ public abstract class AbstractCollectionEvent extends AbstractEvent {
* 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 ) {
public AbstractCollectionEvent( CollectionPersister collectionPersister,
PersistentCollection collection,
EventSource source,
Object affectedOwner,
Serializable affectedOwnerId) {
super(source);
this.collection = collection;
this.affectedOwner = affectedOwner;
this.affectedOwnerId = affectedOwnerId;
this.affectedOwnerEntityName =
getAffectedOwnerEntityName( collectionPersister, affectedOwner, source );
}
protected static CollectionPersister getLoadedCollectionPersister( PersistentCollection collection, EventSource source ) {
CollectionEntry ce = source.getPersistenceContext().getCollectionEntry( collection );
return ( ce == null ? null : ce.getLoadedPersister() );
}
protected static Object getLoadedOwnerOrNull( PersistentCollection collection, EventSource source ) {
@ -51,6 +62,21 @@ public abstract class AbstractCollectionEvent extends AbstractEvent {
return ( ownerEntry == null ? null : ownerEntry.getId() );
}
protected static String getAffectedOwnerEntityName(CollectionPersister collectionPersister, Object affectedOwner, EventSource source ) {
// collectionPersister should not be null, but we don't want to throw
// an exception if it is null
String entityName =
( collectionPersister == null ? null : collectionPersister.getOwnerEntityPersister().getEntityName() );
if ( affectedOwner != null ) {
EntityEntry ee = source.getPersistenceContext().getEntry( affectedOwner );
if ( ee != null && ee.getEntityName() != null) {
entityName = ee.getEntityName();
}
}
return entityName;
}
public PersistentCollection getCollection() {
return collection;
}
@ -75,4 +101,15 @@ public abstract class AbstractCollectionEvent extends AbstractEvent {
public Serializable getAffectedOwnerIdOrNull() {
return affectedOwnerId;
}
/**
* Get the entity name for the collection owner entity that is affected by this event.
*
* @return the entity name; if the owner is not in the PersistenceContext, the
* returned value may be a superclass name, instead of the actual class name
*/
public String getAffectedOwnerEntityName() {
return affectedOwnerEntityName;
}
}

View File

@ -11,8 +11,10 @@ import org.hibernate.collection.PersistentCollection;
*/
public class InitializeCollectionEvent extends AbstractCollectionEvent {
public InitializeCollectionEvent(PersistentCollection collection, EventSource source) {
super( collection, source,
public InitializeCollectionEvent(PersistentCollection collection, EventSource source ) {
super( getLoadedCollectionPersister( collection, source ),
collection,
source,
getLoadedOwnerOrNull( collection, source ),
getLoadedOwnerIdOrNull( collection, source ) );
}

View File

@ -2,6 +2,7 @@
package org.hibernate.event;
import org.hibernate.collection.PersistentCollection;
import org.hibernate.persister.collection.CollectionPersister;
/**
* An event that occurs after a collection is recreated
@ -10,8 +11,10 @@ import org.hibernate.collection.PersistentCollection;
*/
public class PostCollectionRecreateEvent extends AbstractCollectionEvent {
public PostCollectionRecreateEvent(PersistentCollection collection, EventSource source) {
super( collection, source,
public PostCollectionRecreateEvent( CollectionPersister collectionPersister,
PersistentCollection collection,
EventSource source ) {
super( collectionPersister, collection, source,
collection.getOwner(),
getOwnerIdOrNull( collection.getOwner(), source ) );
}

View File

@ -2,6 +2,7 @@
package org.hibernate.event;
import org.hibernate.collection.PersistentCollection;
import org.hibernate.persister.collection.CollectionPersister;
/**
* An event that occurs after a collection is removed
@ -10,8 +11,11 @@ import org.hibernate.collection.PersistentCollection;
*/
public class PostCollectionRemoveEvent extends AbstractCollectionEvent {
public PostCollectionRemoveEvent(PersistentCollection collection, Object loadedOwner, EventSource source) {
super( collection, source,
public PostCollectionRemoveEvent(CollectionPersister collectionPersister,
PersistentCollection collection,
EventSource source,
Object loadedOwner ) {
super( collectionPersister, collection, source,
loadedOwner,
getOwnerIdOrNull( loadedOwner, source ) );
}

View File

@ -2,6 +2,7 @@
package org.hibernate.event;
import org.hibernate.collection.PersistentCollection;
import org.hibernate.persister.collection.CollectionPersister;
/**
* An event that occurs after a collection is updated
@ -10,8 +11,10 @@ import org.hibernate.collection.PersistentCollection;
*/
public class PostCollectionUpdateEvent extends AbstractCollectionEvent {
public PostCollectionUpdateEvent(PersistentCollection collection, EventSource source) {
super( collection, source,
public PostCollectionUpdateEvent(CollectionPersister collectionPersister,
PersistentCollection collection,
EventSource source) {
super( collectionPersister, collection, source,
getLoadedOwnerOrNull( collection, source ),
getLoadedOwnerIdOrNull( collection, source ) );
}

View File

@ -2,6 +2,7 @@
package org.hibernate.event;
import org.hibernate.collection.PersistentCollection;
import org.hibernate.persister.collection.CollectionPersister;
/**
* An event that occurs before a collection is recreated
@ -10,8 +11,10 @@ import org.hibernate.collection.PersistentCollection;
*/
public class PreCollectionRecreateEvent extends AbstractCollectionEvent {
public PreCollectionRecreateEvent(PersistentCollection collection, EventSource source) {
super( collection, source,
public PreCollectionRecreateEvent(CollectionPersister collectionPersister,
PersistentCollection collection,
EventSource source) {
super( collectionPersister, collection, source,
collection.getOwner(),
getOwnerIdOrNull( collection.getOwner(), source ) );
}

View File

@ -2,6 +2,7 @@
package org.hibernate.event;
import org.hibernate.collection.PersistentCollection;
import org.hibernate.persister.collection.CollectionPersister;
/**
* An event that occurs before a collection is removed
@ -10,8 +11,11 @@ import org.hibernate.collection.PersistentCollection;
*/
public class PreCollectionRemoveEvent extends AbstractCollectionEvent {
public PreCollectionRemoveEvent(PersistentCollection collection, Object loadedOwner, EventSource source) {
super( collection, source,
public PreCollectionRemoveEvent(CollectionPersister collectionPersister,
PersistentCollection collection,
EventSource source,
Object loadedOwner) {
super( collectionPersister, collection, source,
loadedOwner,
getOwnerIdOrNull( loadedOwner, source ) );
}

View File

@ -2,6 +2,7 @@
package org.hibernate.event;
import org.hibernate.collection.PersistentCollection;
import org.hibernate.persister.collection.CollectionPersister;
/**
* An event that occurs before a collection is updated
@ -10,8 +11,10 @@ import org.hibernate.collection.PersistentCollection;
*/
public class PreCollectionUpdateEvent extends AbstractCollectionEvent {
public PreCollectionUpdateEvent(PersistentCollection collection, EventSource source) {
super( collection, source,
public PreCollectionUpdateEvent(CollectionPersister collectionPersister,
PersistentCollection collection,
EventSource source) {
super( collectionPersister, collection, source,
getLoadedOwnerOrNull( collection, source ),
getLoadedOwnerIdOrNull( collection, source ) );
}

View File

@ -767,6 +767,10 @@ public abstract class AbstractCollectionEventTest extends FunctionalTestCase {
ownerExpected.getId(),
( ( AbstractCollectionEvent ) listeners.getEvents().get( index ) ).getAffectedOwnerIdOrNull()
);
assertEquals(
ownerExpected.getClass().getName(),
( ( AbstractCollectionEvent ) listeners.getEvents().get( index ) ).getAffectedOwnerEntityName()
);
assertSame(
collExpected, ( ( AbstractCollectionEvent ) listeners.getEvents().get( index ) ).getCollection()
);

View File

@ -317,6 +317,10 @@ public class BrokenCollectionEventTest extends FunctionalTestCase {
ownerExpected.getId(),
( ( AbstractCollectionEvent ) listeners.getEvents().get( index ) ).getAffectedOwnerIdOrNull()
);
assertEquals(
ownerExpected.getClass().getName(),
( ( AbstractCollectionEvent ) listeners.getEvents().get( index ) ).getAffectedOwnerEntityName()
);
assertSame(
collExpected, ( ( AbstractCollectionEvent ) listeners.getEvents().get( index ) ).getCollection()
);

View File

@ -0,0 +1,57 @@
//$Id: $
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* Copyright (c) 2007, Red Hat Middleware LLC or third-party contributors as
* indicated by the @author tags or express copyright attribution statements
* applied by the authors.
*
* All third-party contributions are distributed under license by Red Hat
* Middleware LLC. This copyrighted material is made available to anyone
* wishing to use, modify, copy, or redistribute it subject to the terms
* and conditions of the GNU Lesser General Public License, as published by
* the Free Software Foundation. This program is distributed in the hope
* that it will be useful, but WITHOUT ANY WARRANTY; without even the
* implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*
* See the GNU Lesser General Public License for more details. You should
* have received a copy of the GNU Lesser General Public License along with
* this distribution; if not, write to: Free Software Foundation, Inc.
* 51 Franklin Street, Fifth Floor Boston, MA 02110-1301 USA
*/
package org.hibernate.test.event.collection.association.bidirectional.onetomany;
import java.util.ArrayList;
import java.util.Collection;
import junit.framework.Test;
import org.hibernate.junit.functional.FunctionalTestClassTestSuite;
import org.hibernate.test.event.collection.Child;
import org.hibernate.test.event.collection.ParentWithCollection;
import org.hibernate.test.event.collection.association.AbstractAssociationCollectionEventTest;
import org.hibernate.test.event.collection.association.bidirectional.onetomany.ChildWithManyToOne;
import org.hibernate.test.event.collection.association.bidirectional.onetomany.ParentWithBidirectionalOneToMany;
/**
*
* @author Gail Badner
*/
public class BidirectionalOneToManyBagSubclassCollectionEventTest extends BidirectionalOneToManyBagCollectionEventTest {
public BidirectionalOneToManyBagSubclassCollectionEventTest(String string) {
super( string );
}
public static Test suite() {
return new FunctionalTestClassTestSuite( BidirectionalOneToManyBagSubclassCollectionEventTest.class );
}
public String[] getMappings() {
return new String[] { "event/collection/association/bidirectional/onetomany/BidirectionalOneToManyBagSubclassMapping.hbm.xml" };
}
public ParentWithCollection createParent(String name) {
return new ParentWithBidirectionalOneToManySubclass( name );
}
}

View File

@ -0,0 +1,39 @@
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!--
-->
<hibernate-mapping package="org.hibernate.test.event.collection.association.bidirectional.onetomany">
<class name="ParentWithBidirectionalOneToMany" table="PARENT" discriminator-value="P">
<id name="id" column="ID" type="long">
<generator class="native"/>
</id>
<discriminator column="TYPE" type="string" length="1"/>
<bag name="children"
inverse="true"
cascade="all">
<key column="parent_id"/>
<one-to-many class="ChildWithManyToOne"/>
</bag>
<subclass name="ParentWithBidirectionalOneToManySubclass"
discriminator-value="S">
</subclass>
</class>
<class name="ChildWithManyToOne" table="CHILD">
<id name="id" column="ID" type="long">
<generator class="native"/>
</id>
<property name="name" column="NAME" type="string"/>
<many-to-one name="parent" column="parent_id" class="ParentWithBidirectionalOneToMany" cascade="none"/>
</class>
</hibernate-mapping>

View File

@ -0,0 +1,42 @@
//$Id: $
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* Copyright (c) 2007, Red Hat Middleware LLC or third-party contributors as
* indicated by the @author tags or express copyright attribution statements
* applied by the authors.
*
* All third-party contributions are distributed under license by Red Hat
* Middleware LLC. This copyrighted material is made available to anyone
* wishing to use, modify, copy, or redistribute it subject to the terms
* and conditions of the GNU Lesser General Public License, as published by
* the Free Software Foundation. This program is distributed in the hope
* that it will be useful, but WITHOUT ANY WARRANTY; without even the
* implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*
* See the GNU Lesser General Public License for more details. You should
* have received a copy of the GNU Lesser General Public License along with
* this distribution; if not, write to: Free Software Foundation, Inc.
* 51 Franklin Street, Fifth Floor Boston, MA 02110-1301 USA
*/
package org.hibernate.test.event.collection.association.bidirectional.onetomany;
import java.util.Collection;
import java.util.Iterator;
import org.hibernate.test.event.collection.Child;
import org.hibernate.test.event.collection.AbstractParentWithCollection;
/**
*
* @author Gail Badner
*/
public class ParentWithBidirectionalOneToManySubclass extends ParentWithBidirectionalOneToMany {
public ParentWithBidirectionalOneToManySubclass() {
}
public ParentWithBidirectionalOneToManySubclass(String name) {
super( name );
}
}