org.hibernate.action.internal.CollectionRecreateAction Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of hibernate-core Show documentation
Show all versions of hibernate-core Show documentation
Hibernate's core ORM functionality
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
* See the lgpl.txt file in the root directory or .
*/
package org.hibernate.action.internal;
import org.hibernate.HibernateException;
import org.hibernate.collection.spi.PersistentCollection;
import org.hibernate.engine.spi.SharedSessionContractImplementor;
import org.hibernate.event.service.spi.EventListenerGroup;
import org.hibernate.event.spi.EventType;
import org.hibernate.event.spi.PostCollectionRecreateEvent;
import org.hibernate.event.spi.PostCollectionRecreateEventListener;
import org.hibernate.event.spi.PreCollectionRecreateEvent;
import org.hibernate.event.spi.PreCollectionRecreateEventListener;
import org.hibernate.metamodel.model.domain.spi.PersistentCollectionDescriptor;
/**
* The action for recreating a collection
*/
public final class CollectionRecreateAction extends CollectionAction {
/**
* Constructs a CollectionRecreateAction
* @param collection The collection being recreated
* @param collectionDescriptor The collection persister
* @param collectionKey The collection key
* @param session The session
*/
public CollectionRecreateAction(
final PersistentCollection collection,
final PersistentCollectionDescriptor collectionDescriptor,
final Object collectionKey,
final SharedSessionContractImplementor session) {
super( collectionDescriptor, collection, collectionKey, session );
}
@Override
public void execute() throws HibernateException {
// this method is called when a new non-null collection is persisted
// or when an existing (non-null) collection is moved to a new owner
final PersistentCollection collection = getCollection();
preRecreate();
getPersistentCollectionDescriptor().recreate( collection, getKey(), getSession() );
getSession().getPersistenceContext().getCollectionEntry( collection ).afterAction( collection );
evict();
postRecreate();
if ( getSession().getFactory().getStatistics().isStatisticsEnabled() ) {
getSession().getFactory().getStatistics().recreateCollection( getPersistentCollectionDescriptor().getNavigableRole().getNavigableName() );
}
}
private void preRecreate() {
final EventListenerGroup listenerGroup = listenerGroup( EventType.PRE_COLLECTION_RECREATE );
if ( listenerGroup.isEmpty() ) {
return;
}
final PreCollectionRecreateEvent event = new PreCollectionRecreateEvent( getPersistentCollectionDescriptor(), getCollection(), eventSource() );
for ( PreCollectionRecreateEventListener listener : listenerGroup.listeners() ) {
listener.onPreRecreateCollection( event );
}
}
private void postRecreate() {
final EventListenerGroup listenerGroup = listenerGroup( EventType.POST_COLLECTION_RECREATE );
if ( listenerGroup.isEmpty() ) {
return;
}
final PostCollectionRecreateEvent event = new PostCollectionRecreateEvent( getPersistentCollectionDescriptor(), getCollection(), eventSource() );
for ( PostCollectionRecreateEventListener listener : listenerGroup.listeners() ) {
listener.onPostRecreateCollection( event );
}
}
}