FileDocCategorySizeDatePackage
EntityIdentityInsertAction.javaAPI DocHibernate 3.2.55367Wed Nov 01 15:53:04 GMT 2006org.hibernate.action

EntityIdentityInsertAction

public final class EntityIdentityInsertAction extends EntityAction

Fields Summary
private final Object[]
state
private final boolean
isDelayed
private final org.hibernate.engine.EntityKey
delayedEntityKey
private Serializable
generatedId
Constructors Summary
public EntityIdentityInsertAction(Object[] state, Object instance, org.hibernate.persister.entity.EntityPersister persister, org.hibernate.engine.SessionImplementor session, boolean isDelayed)

		super( session, null, instance, persister );
		this.state = state;
		this.isDelayed = isDelayed;
		delayedEntityKey = isDelayed ? generateDelayedEntityKey() : null;
	
Methods Summary
public voidafterTransactionCompletion(boolean success)

		//TODO: reenable if we also fix the above todo
		/*EntityPersister persister = getEntityPersister();
		if ( success && persister.hasCache() && !persister.isCacheInvalidationRequired() ) {
			persister.getCache().afterInsert( getGeneratedId(), cacheEntry );
		}*/
		postCommitInsert();
	
public voidexecute()

		
		final EntityPersister persister = getPersister();
		final SessionImplementor session = getSession();
		final Object instance = getInstance();
		
		boolean veto = preInsert();

		// Don't need to lock the cache here, since if someone
		// else inserted the same pk first, the insert would fail

		if ( !veto ) {
			generatedId = persister.insert( state, instance, session );
			if ( persister.hasInsertGeneratedProperties() ) {
				persister.processInsertGeneratedProperties( generatedId, instance, state, session );
			}
			//need to do that here rather than in the save event listener to let
			//the post insert events to have a id-filled entity when IDENTITY is used (EJB3)
			persister.setIdentifier( instance, generatedId, session.getEntityMode() );
		}


		//TODO: this bit actually has to be called after all cascades!
		//      but since identity insert is called *synchronously*,
		//      instead of asynchronously as other actions, it isn't
		/*if ( persister.hasCache() && !persister.isCacheInvalidationRequired() ) {
			cacheEntry = new CacheEntry(object, persister, session);
			persister.getCache().insert(generatedId, cacheEntry);
		}*/
		
		postInsert();

		if ( session.getFactory().getStatistics().isStatisticsEnabled() && !veto ) {
			session.getFactory().getStatisticsImplementor()
					.insertEntity( getPersister().getEntityName() );
		}

	
private synchronized org.hibernate.engine.EntityKeygenerateDelayedEntityKey()

		if ( !isDelayed ) {
			throw new AssertionFailure( "cannot request delayed entity-key for non-delayed post-insert-id generation" );
		}
		return new EntityKey( new DelayedPostInsertIdentifier(), getPersister(), getSession().getEntityMode() );
	
public org.hibernate.engine.EntityKeygetDelayedEntityKey()

		return delayedEntityKey;
	
public final java.io.SerializablegetGeneratedId()

		return generatedId;
	
public booleanhasAfterTransactionCompletion()

		//TODO: simply remove this override
		//      if we fix the above todos
		return hasPostCommitEventListeners();
	
protected booleanhasPostCommitEventListeners()

		return getSession().getListeners().getPostCommitInsertEventListeners().length>0;
	
private voidpostCommitInsert()

		PostInsertEventListener[] postListeners = getSession().getListeners()
				.getPostCommitInsertEventListeners();
		if (postListeners.length>0) {
			PostInsertEvent postEvent = new PostInsertEvent(
					getInstance(),
					generatedId,
					state,
					getPersister(),
					(EventSource) getSession() 
			);
			for ( int i = 0; i < postListeners.length; i++ ) {
				postListeners[i].onPostInsert(postEvent);
			}
		}
	
private voidpostInsert()

		if ( isDelayed ) {
			getSession().getPersistenceContext().replaceDelayedEntityIdentityInsertKeys( delayedEntityKey, generatedId );
		}
		PostInsertEventListener[] postListeners = getSession().getListeners()
				.getPostInsertEventListeners();
		if (postListeners.length>0) {
			PostInsertEvent postEvent = new PostInsertEvent(
					getInstance(),
					generatedId,
					state,
					getPersister(),
					(EventSource) getSession()
			);
			for ( int i = 0; i < postListeners.length; i++ ) {
				postListeners[i].onPostInsert(postEvent);
			}
		}
	
private booleanpreInsert()

		PreInsertEventListener[] preListeners = getSession().getListeners()
				.getPreInsertEventListeners();
		boolean veto = false;
		if (preListeners.length>0) {
			PreInsertEvent preEvent = new PreInsertEvent( getInstance(), null, state, getPersister(), getSession() );
			for ( int i = 0; i < preListeners.length; i++ ) {
				veto = preListeners[i].onPreInsert(preEvent) || veto;
			}
		}
		return veto;