package org.hibernate.action.internal;
import java.io.Serializable;
import org.hibernate.AssertionFailure;
import org.hibernate.HibernateException;
import org.hibernate.engine.spi.EntityKey;
import org.hibernate.engine.spi.SharedSessionContractImplementor;
import org.hibernate.event.service.spi.EventListenerGroup;
import org.hibernate.event.spi.EventType;
import org.hibernate.event.spi.PostCommitInsertEventListener;
import org.hibernate.event.spi.PostInsertEvent;
import org.hibernate.event.spi.PostInsertEventListener;
import org.hibernate.event.spi.PreInsertEvent;
import org.hibernate.event.spi.PreInsertEventListener;
import org.hibernate.persister.entity.EntityPersister;
public final class EntityIdentityInsertAction extends AbstractEntityInsertAction {
private final boolean isDelayed;
private final EntityKey delayedEntityKey;
private EntityKey entityKey;
private Serializable generatedId;
public EntityIdentityInsertAction(
Object[] state,
Object instance,
EntityPersister persister,
boolean isVersionIncrementDisabled,
SharedSessionContractImplementor session,
boolean isDelayed) {
super(
( isDelayed ? generateDelayedPostInsertIdentifier() : null ),
state,
instance,
isVersionIncrementDisabled,
persister,
session
);
this.isDelayed = isDelayed;
this.delayedEntityKey = isDelayed ? generateDelayedEntityKey() : null;
}
@Override
public void execute() throws HibernateException {
nullifyTransientReferencesIfNotAlready();
final EntityPersister persister = getPersister();
final SharedSessionContractImplementor session = getSession();
final Object instance = getInstance();
setVeto( preInsert() );
if ( !isVeto() ) {
generatedId = persister.insert( getState(), instance, session );
if ( persister.hasInsertGeneratedProperties() ) {
persister.processInsertGeneratedProperties( generatedId, instance, getState(), session );
}
persister.setIdentifier( instance, generatedId, session );
session.getPersistenceContext().registerInsertedKey( getPersister(), generatedId );
entityKey = session.generateEntityKey( generatedId, persister );
session.getPersistenceContext().checkUniqueness( entityKey, getInstance() );
}
postInsert();
if ( session.getFactory().getStatistics().isStatisticsEnabled() && !isVeto() ) {
session.getFactory().getStatistics().insertEntity( getPersister().getEntityName() );
}
markExecuted();
}
@Override
public boolean needsAfterTransactionCompletion() {
return hasPostCommitEventListeners();
}
@Override
protected boolean hasPostCommitEventListeners() {
final EventListenerGroup<PostInsertEventListener> group = listenerGroup( EventType.POST_COMMIT_INSERT );
for ( PostInsertEventListener listener : group.listeners() ) {
if ( listener.requiresPostCommitHandling( getPersister() ) ) {
return true;
}
}
return false;
}
@Override
public void doAfterTransactionCompletion(boolean success, SharedSessionContractImplementor session) {
postCommitInsert( success );
}
private void postInsert() {
if ( isDelayed ) {
getSession().getPersistenceContext().replaceDelayedEntityIdentityInsertKeys( delayedEntityKey, generatedId );
}
final EventListenerGroup<PostInsertEventListener> listenerGroup = listenerGroup( EventType.POST_INSERT );
if ( listenerGroup.isEmpty() ) {
return;
}
final PostInsertEvent event = new PostInsertEvent(
getInstance(),
generatedId,
getState(),
getPersister(),
eventSource()
);
for ( PostInsertEventListener listener : listenerGroup.listeners() ) {
listener.onPostInsert( event );
}
}
private void postCommitInsert(boolean success) {
final EventListenerGroup<PostInsertEventListener> listenerGroup = listenerGroup( EventType.POST_COMMIT_INSERT );
if ( listenerGroup.isEmpty() ) {
return;
}
final PostInsertEvent event = new PostInsertEvent(
getInstance(),
generatedId,
getState(),
getPersister(),
eventSource()
);
for ( PostInsertEventListener listener : listenerGroup.listeners() ) {
if ( PostCommitInsertEventListener.class.isInstance( listener ) ) {
if ( success ) {
listener.onPostInsert( event );
}
else {
((PostCommitInsertEventListener) listener).onPostInsertCommitFailed( event );
}
}
else {
listener.onPostInsert( event );
}
}
}
private boolean preInsert() {
final EventListenerGroup<PreInsertEventListener> listenerGroup = listenerGroup( EventType.PRE_INSERT );
if ( listenerGroup.isEmpty() ) {
return false;
}
boolean veto = false;
final PreInsertEvent event = new PreInsertEvent( getInstance(), null, getState(), getPersister(), eventSource() );
for ( PreInsertEventListener listener : listenerGroup.listeners() ) {
veto |= listener.onPreInsert( event );
}
return veto;
}
public final Serializable getGeneratedId() {
return generatedId;
}
@Deprecated
@SuppressWarnings("UnusedDeclaration")
public EntityKey getDelayedEntityKey() {
return delayedEntityKey;
}
@Override
public boolean isEarlyInsert() {
return !isDelayed;
}
@Override
protected EntityKey getEntityKey() {
return entityKey != null ? entityKey : delayedEntityKey;
}
private static DelayedPostInsertIdentifier generateDelayedPostInsertIdentifier() {
return new DelayedPostInsertIdentifier();
}
private EntityKey generateDelayedEntityKey() {
if ( !isDelayed ) {
throw new AssertionFailure( "cannot request delayed entity-key for early-insert post-insert-id generation" );
}
return getSession().generateEntityKey( getDelayedId(), getPersister() );
}
}