FileDocCategorySizeDatePackage
DefaultLockEventListener.javaAPI DocHibernate 3.2.52678Sun Jun 05 00:09:58 BST 2005org.hibernate.event.def

DefaultLockEventListener

public class DefaultLockEventListener extends AbstractLockUpgradeEventListener implements org.hibernate.event.LockEventListener
Defines the default lock event listeners used by hibernate to lock entities in response to generated lock events.
author
Steve Ebersole

Fields Summary
Constructors Summary
Methods Summary
private voidcascadeOnLock(org.hibernate.event.LockEvent event, org.hibernate.persister.entity.EntityPersister persister, java.lang.Object entity)

		EventSource source = event.getSession();
		source.getPersistenceContext().incrementCascadeLevel();
		try {
			new Cascade(CascadingAction.LOCK, Cascade.AFTER_LOCK, source)
					.cascade( persister, entity, event.getLockMode() );
		}
		finally {
			source.getPersistenceContext().decrementCascadeLevel();
		}
	
public voidonLock(org.hibernate.event.LockEvent event)
Handle the given lock event.

param
event The lock event to be handled.
throws
HibernateException


		if ( event.getObject() == null ) {
			throw new NullPointerException( "attempted to lock null" );
		}

		if ( event.getLockMode() == LockMode.WRITE ) {
			throw new HibernateException( "Invalid lock mode for lock()" );
		}

		SessionImplementor source = event.getSession();
		
		Object entity = source.getPersistenceContext().unproxyAndReassociate( event.getObject() );
		//TODO: if object was an uninitialized proxy, this is inefficient,
		//      resulting in two SQL selects
		
		EntityEntry entry = source.getPersistenceContext().getEntry(entity);
		if (entry==null) {
			final EntityPersister persister = source.getEntityPersister( event.getEntityName(), entity );
			final Serializable id = persister.getIdentifier( entity, source.getEntityMode() );
			if ( !ForeignKeys.isNotTransient( event.getEntityName(), entity, Boolean.FALSE, source ) ) {
				throw new TransientObjectException(
						"cannot lock an unsaved transient instance: " +
						persister.getEntityName()
				);
			}

			entry = reassociate(event, entity, id, persister);
			
			cascadeOnLock(event, persister, entity);
		}

		upgradeLock( entity, entry, event.getLockMode(), source );