Java Code Examples for org.hibernate.engine.spi.Status#DELETED

The following examples show how to use org.hibernate.engine.spi.Status#DELETED . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DefaultSaveOrUpdateEventListener.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
/**
 * The given save-update event named a transient entity.
 * <p/>
 * Here, we will perform the save processing.
 *
 * @param event The save event to be handled.
 *
 * @return The entity's identifier after saving.
 */
protected Serializable entityIsTransient(SaveOrUpdateEvent event) {

	LOG.trace( "Saving transient instance" );

	final EventSource source = event.getSession();

	EntityEntry entityEntry = event.getEntry();
	if ( entityEntry != null ) {
		if ( entityEntry.getStatus() == Status.DELETED ) {
			source.forceFlush( entityEntry );
		}
		else {
			throw new AssertionFailure( "entity was persistent" );
		}
	}

	Serializable id = saveWithGeneratedOrRequestedId( event );

	source.getPersistenceContext().reassociateProxy( event.getObject(), id );

	return id;
}
 
Example 2
Source File: DefaultFlushEntityEventListener.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
protected boolean invokeInterceptor(
		SessionImplementor session,
		Object entity,
		EntityEntry entry,
		final Object[] values,
		EntityPersister persister) {
	boolean isDirty = false;
	if ( entry.getStatus() != Status.DELETED ) {
		if ( callbackRegistry.preUpdate( entity ) ) {
			isDirty = copyState( entity, persister.getPropertyTypes(), values, session.getFactory() );
		}
	}

	final boolean answerFromInterceptor =  session.getInterceptor().onFlushDirty(
			entity,
			entry.getId(),
			values,
			entry.getLoadedState(),
			persister.getPropertyNames(),
			persister.getPropertyTypes()
	);

	return answerFromInterceptor || isDirty;
}
 
Example 3
Source File: DefaultFlushEntityEventListener.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
private boolean isUpdateNecessary(final FlushEntityEvent event, final boolean mightBeDirty) {
	final Status status = event.getEntityEntry().getStatus();
	if ( mightBeDirty || status == Status.DELETED ) {
		// compare to cached state (ignoring collections unless versioned)
		dirtyCheck( event );
		if ( isUpdateNecessary( event ) ) {
			return true;
		}
		else {
			if ( SelfDirtinessTracker.class.isInstance( event.getEntity() ) ) {
				( (SelfDirtinessTracker) event.getEntity() ).$$_hibernate_clearDirtyAttributes();
			}
			event.getSession()
					.getFactory()
					.getCustomEntityDirtinessStrategy()
					.resetDirty( event.getEntity(), event.getEntityEntry().getPersister(), event.getSession() );
			return false;
		}
	}
	else {
		return hasDirtyCollections( event, event.getEntityEntry().getPersister(), status );
	}
}
 
Example 4
Source File: DefaultFlushEntityEventListener.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
private Object[] getValues(Object entity, EntityEntry entry, boolean mightBeDirty, SessionImplementor session) {
	final Object[] loadedState = entry.getLoadedState();
	final Status status = entry.getStatus();
	final EntityPersister persister = entry.getPersister();

	final Object[] values;
	if ( status == Status.DELETED ) {
		//grab its state saved at deletion
		values = entry.getDeletedState();
	}
	else if ( !mightBeDirty && loadedState != null ) {
		values = loadedState;
	}
	else {
		checkId( entity, persister, entry.getId(), session );

		// grab its current state
		values = persister.getPropertyValues( entity );

		checkNaturalId( persister, entry, values, loadedState, session );
	}
	return values;
}
 
Example 5
Source File: AbstractSaveEventListener.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Determine whether the entity is persistent, detached, or transient
 *
 * @param entity The entity to check
 * @param entityName The name of the entity
 * @param entry The entity's entry in the persistence context
 * @param source The originating session.
 *
 * @return The state.
 */
protected EntityState getEntityState(
		Object entity,
		String entityName,
		EntityEntry entry, //pass this as an argument only to avoid double looking
		SessionImplementor source) {

	final boolean traceEnabled = LOG.isTraceEnabled();
	if ( entry != null ) { // the object is persistent

		//the entity is associated with the session, so check its status
		if ( entry.getStatus() != Status.DELETED ) {
			// do nothing for persistent instances
			if ( traceEnabled ) {
				LOG.tracev( "Persistent instance of: {0}", getLoggableName( entityName, entity ) );
			}
			return EntityState.PERSISTENT;
		}
		// ie. e.status==DELETED
		if ( traceEnabled ) {
			LOG.tracev( "Deleted instance of: {0}", getLoggableName( entityName, entity ) );
		}
		return EntityState.DELETED;
	}
	// the object is transient or detached

	// the entity is not associated with the session, so
	// try interceptor and unsaved-value

	if ( ForeignKeys.isTransient( entityName, entity, getAssumedUnsaved(), source ) ) {
		if ( traceEnabled ) {
			LOG.tracev( "Transient instance of: {0}", getLoggableName( entityName, entity ) );
		}
		return EntityState.TRANSIENT;
	}
	if ( traceEnabled ) {
		LOG.tracev( "Detached instance of: {0}", getLoggableName( entityName, entity ) );
	}
	return EntityState.DETACHED;
}
 
Example 6
Source File: DefaultReactiveLoadEventListener.java    From hibernate-reactive with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * If there is already a corresponding proxy associated with the
 * persistence context, return it; otherwise create a proxy, associate it
 * with the persistence context, and return the just-created proxy.
 *
 * @param event The initiating load request event
 * @param persister The persister corresponding to the entity to be loaded
 * @param keyToLoad The key of the entity to be loaded
 * @param options The defined load options
 * @param persistenceContext The originating session
 *
 * @return The created/existing proxy
 */
private Object createProxyIfNecessary(
		final LoadEvent event,
		final EntityPersister persister,
		final EntityKey keyToLoad,
		final LoadEventListener.LoadType options,
		final PersistenceContext persistenceContext) {
	Object existing = persistenceContext.getEntity( keyToLoad );
	final boolean traceEnabled = LOG.isTraceEnabled();
	if ( existing != null ) {
		// return existing object or initialized proxy (unless deleted)
		if ( traceEnabled ) {
			LOG.trace( "Entity found in session cache" );
		}
		if ( options.isCheckDeleted() ) {
			EntityEntry entry = persistenceContext.getEntry( existing );
			Status status = entry.getStatus();
			if ( status == Status.DELETED || status == Status.GONE ) {
				return null;
			}
		}
		return existing;
	}
	if ( traceEnabled ) {
		LOG.trace( "Creating new proxy for entity" );
	}
	return createProxy( event, persister, keyToLoad, persistenceContext );
}
 
Example 7
Source File: DefaultLoadEventListener.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Attempts to locate the entity in the session-level cache.
 * <p/>
 * If allowed to return nulls, then if the entity happens to be found in
 * the session cache, we check the entity type for proper handling
 * of entity hierarchies.
 * <p/>
 * If checkDeleted was set to true, then if the entity is found in the
 * session-level cache, it's current status within the session cache
 * is checked to see if it has previously been scheduled for deletion.
 *
 * @param event The load event
 * @param keyToLoad The EntityKey representing the entity to be loaded.
 * @param options The load options.
 *
 * @return The entity from the session-level cache, or null.
 *
 * @throws HibernateException Generally indicates problems applying a lock-mode.
 */
protected Object loadFromSessionCache(
		final LoadEvent event,
		final EntityKey keyToLoad,
		final LoadEventListener.LoadType options) throws HibernateException {

	SessionImplementor session = event.getSession();
	Object old = session.getEntityUsingInterceptor( keyToLoad );

	if ( old != null ) {
		// this object was already loaded
		EntityEntry oldEntry = session.getPersistenceContext().getEntry( old );
		if ( options.isCheckDeleted() ) {
			Status status = oldEntry.getStatus();
			if ( status == Status.DELETED || status == Status.GONE ) {
				return REMOVED_ENTITY_MARKER;
			}
		}
		if ( options.isAllowNulls() ) {
			final EntityPersister persister = event.getSession()
					.getFactory()
					.getEntityPersister( keyToLoad.getEntityName() );
			if ( !persister.isInstance( old ) ) {
				return INCONSISTENT_RTN_CLASS_MARKER;
			}
		}
		upgradeLock( old, oldEntry, event.getLockOptions(), event.getSession() );
	}

	return old;
}
 
Example 8
Source File: DefaultLoadEventListener.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * If there is already a corresponding proxy associated with the
 * persistence context, return it; otherwise create a proxy, associate it
 * with the persistence context, and return the just-created proxy.
 *
 * @param event The initiating load request event
 * @param persister The persister corresponding to the entity to be loaded
 * @param keyToLoad The key of the entity to be loaded
 * @param options The defined load options
 * @param persistenceContext The originating session
 *
 * @return The created/existing proxy
 */
private Object createProxyIfNecessary(
		final LoadEvent event,
		final EntityPersister persister,
		final EntityKey keyToLoad,
		final LoadEventListener.LoadType options,
		final PersistenceContext persistenceContext) {
	Object existing = persistenceContext.getEntity( keyToLoad );
	if ( existing != null ) {
		// return existing object or initialized proxy (unless deleted)
		if ( traceEnabled ) {
			LOG.trace( "Entity found in session cache" );
		}
		if ( options.isCheckDeleted() ) {
			EntityEntry entry = persistenceContext.getEntry( existing );
			Status status = entry.getStatus();
			if ( status == Status.DELETED || status == Status.GONE ) {
				return null;
			}
		}
		return existing;
	}
	if ( traceEnabled ) {
		LOG.trace( "Creating new proxy for entity" );
	}
	// return new uninitialized proxy
	Object proxy = persister.createProxy( event.getEntityId(), event.getSession() );
	persistenceContext.getBatchFetchQueue().addBatchLoadableEntityKey( keyToLoad );
	persistenceContext.addProxy( keyToLoad, proxy );
	return proxy;
}
 
Example 9
Source File: PostUpdateEventListenerStandardImpl.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
private void handlePostUpdate(Object entity, EventSource source) {
	EntityEntry entry = source.getPersistenceContext().getEntry( entity );
	// mimic the preUpdate filter
	if ( Status.DELETED != entry.getStatus()) {
		callbackRegistry.postUpdate(entity);
	}
}
 
Example 10
Source File: DefaultSaveEventListener.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
protected Serializable performSaveOrUpdate(SaveOrUpdateEvent event) {
	// this implementation is supposed to tolerate incorrect unsaved-value
	// mappings, for the purpose of backward-compatibility
	EntityEntry entry = event.getSession().getPersistenceContext().getEntry( event.getEntity() );
	if ( entry!=null && entry.getStatus() != Status.DELETED ) {
		return entityIsPersistent(event);
	}
	else {
		return entityIsTransient(event);
	}
}
 
Example 11
Source File: AbstractEntityEntry.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
public boolean isModifiableEntity() {
	final Status status = getStatus();
	final Status previousStatus = getPreviousStatus();
	return getPersister().isMutable()
			&& status != Status.READ_ONLY
			&& ! ( status == Status.DELETED && previousStatus == Status.READ_ONLY );
}
 
Example 12
Source File: OptimisticLockingChildUpdatesRootVersionTest.java    From high-performance-java-persistence with Apache License 2.0 4 votes vote down vote up
private boolean deleted(FlushEntityEvent event) {
    return event.getEntityEntry().getStatus() == Status.DELETED;
}
 
Example 13
Source File: DefaultDeleteEventListener.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Handle the given delete event.  This is the cascaded form.
 *
 * @param event The delete event.
 * @param transientEntities The cache of entities already deleted
 *
 * @throws HibernateException
 */
public void onDelete(DeleteEvent event, Set transientEntities) throws HibernateException {

	final EventSource source = event.getSession();

	final PersistenceContext persistenceContext = source.getPersistenceContext();
	Object entity = persistenceContext.unproxyAndReassociate( event.getObject() );

	EntityEntry entityEntry = persistenceContext.getEntry( entity );
	final EntityPersister persister;
	final Serializable id;
	final Object version;

	if ( entityEntry == null ) {
		LOG.trace( "Entity was not persistent in delete processing" );

		persister = source.getEntityPersister( event.getEntityName(), entity );

		if ( ForeignKeys.isTransient( persister.getEntityName(), entity, null, source ) ) {
			deleteTransientEntity( source, entity, event.isCascadeDeleteEnabled(), persister, transientEntities );
			// EARLY EXIT!!!
			return;
		}
		performDetachedEntityDeletionCheck( event );

		id = persister.getIdentifier( entity, source );

		if ( id == null ) {
			throw new TransientObjectException(
					"the detached instance passed to delete() had a null identifier"
			);
		}

		final EntityKey key = source.generateEntityKey( id, persister );

		persistenceContext.checkUniqueness( key, entity );

		new OnUpdateVisitor( source, id, entity ).process( entity, persister );

		version = persister.getVersion( entity );

		entityEntry = persistenceContext.addEntity(
				entity,
				(persister.isMutable() ? Status.MANAGED : Status.READ_ONLY),
				persister.getPropertyValues( entity ),
				key,
				version,
				LockMode.NONE,
				true,
				persister,
				false
		);
	}
	else {
		LOG.trace( "Deleting a persistent instance" );

		if ( entityEntry.getStatus() == Status.DELETED || entityEntry.getStatus() == Status.GONE ) {
			LOG.trace( "Object was already deleted" );
			return;
		}
		persister = entityEntry.getPersister();
		id = entityEntry.getId();
		version = entityEntry.getVersion();
	}

	/*if ( !persister.isMutable() ) {
		throw new HibernateException(
				"attempted to delete an object of immutable class: " +
				MessageHelper.infoString(persister)
			);
	}*/

	if ( invokeDeleteLifecycle( source, entity, persister ) ) {
		return;
	}

	deleteEntity(
			source,
			entity,
			entityEntry,
			event.isCascadeDeleteEnabled(),
			event.isOrphanRemovalBeforeUpdates(),
			persister,
			transientEntities
	);

	if ( source.getFactory().getSettings().isIdentifierRollbackEnabled() ) {
		persister.resetIdentifier( entity, id, version, source );
	}
}
 
Example 14
Source File: DefaultFlushEntityEventListener.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
private boolean scheduleUpdate(final FlushEntityEvent event) {
	final EntityEntry entry = event.getEntityEntry();
	final EventSource session = event.getSession();
	final Object entity = event.getEntity();
	final Status status = entry.getStatus();
	final EntityPersister persister = entry.getPersister();
	final Object[] values = event.getPropertyValues();

	if ( LOG.isTraceEnabled() ) {
		if ( status == Status.DELETED ) {
			if ( !persister.isMutable() ) {
				LOG.tracev(
						"Updating immutable, deleted entity: {0}",
						MessageHelper.infoString( persister, entry.getId(), session.getFactory() )
				);
			}
			else if ( !entry.isModifiableEntity() ) {
				LOG.tracev(
						"Updating non-modifiable, deleted entity: {0}",
						MessageHelper.infoString( persister, entry.getId(), session.getFactory() )
				);
			}
			else {
				LOG.tracev(
						"Updating deleted entity: ",
						MessageHelper.infoString( persister, entry.getId(), session.getFactory() )
				);
			}
		}
		else {
			LOG.tracev(
					"Updating entity: {0}",
					MessageHelper.infoString( persister, entry.getId(), session.getFactory() )
			);
		}
	}

	final boolean intercepted = !entry.isBeingReplicated() && handleInterception( event );

	// increment the version number (if necessary)
	final Object nextVersion = getNextVersion( event );

	// if it was dirtied by a collection only
	int[] dirtyProperties = event.getDirtyProperties();
	if ( event.isDirtyCheckPossible() && dirtyProperties == null ) {
		if ( !intercepted && !event.hasDirtyCollection() ) {
			throw new AssertionFailure( "dirty, but no dirty properties" );
		}
		dirtyProperties = ArrayHelper.EMPTY_INT_ARRAY;
	}

	// check nullability but do not doAfterTransactionCompletion command execute
	// we'll use scheduled updates for that.
	new Nullability( session ).checkNullability( values, persister, true );

	// schedule the update
	// note that we intentionally do _not_ pass in currentPersistentState!
	session.getActionQueue().addAction(
			new EntityUpdateAction(
					entry.getId(),
					values,
					dirtyProperties,
					event.hasDirtyCollection(),
					( status == Status.DELETED && !entry.isModifiableEntity() ?
							persister.getPropertyValues( entity ) :
							entry.getLoadedState() ),
					entry.getVersion(),
					nextVersion,
					entity,
					entry.getRowId(),
					persister,
					session
			)
	);

	return intercepted;
}
 
Example 15
Source File: DefaultSaveOrUpdateEventListener.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
protected Serializable entityIsPersistent(SaveOrUpdateEvent event) throws HibernateException {
	final boolean traceEnabled = LOG.isTraceEnabled();
	if ( traceEnabled ) {
		LOG.trace( "Ignoring persistent instance" );
	}
	EntityEntry entityEntry = event.getEntry();
	if ( entityEntry == null ) {
		throw new AssertionFailure( "entity was transient or detached" );
	}
	else {

		if ( entityEntry.getStatus() == Status.DELETED ) {
			throw new AssertionFailure( "entity was deleted" );
		}

		final SessionFactoryImplementor factory = event.getSession().getFactory();

		Serializable requestedId = event.getRequestedId();

		Serializable savedId;
		if ( requestedId == null ) {
			savedId = entityEntry.getId();
		}
		else {

			final boolean isEqual = !entityEntry.getPersister().getIdentifierType()
					.isEqual( requestedId, entityEntry.getId(), factory );

			if ( isEqual ) {
				throw new PersistentObjectException(
						"object passed to save() was already persistent: " +
								MessageHelper.infoString( entityEntry.getPersister(), requestedId, factory )
				);
			}

			savedId = requestedId;

		}

		if ( traceEnabled ) {
			LOG.tracev(
					"Object already associated with session: {0}",
					MessageHelper.infoString( entityEntry.getPersister(), savedId, factory )
			);
		}

		return savedId;

	}
}
 
Example 16
Source File: OptimisticLockingBidirectionalChildUpdatesRootVersionTest.java    From high-performance-java-persistence with Apache License 2.0 4 votes vote down vote up
private void incrementRootVersion(FlushEntityEvent event, Object root) {
    EntityEntry entityEntry = event.getSession().getPersistenceContext().getEntry( Hibernate.unproxy( root) );
    if(entityEntry.getStatus() != Status.DELETED) {
        event.getSession().lock(root, LockMode.OPTIMISTIC_FORCE_INCREMENT);
    }
}
 
Example 17
Source File: StatefulPersistenceContext.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
private void setHasNonReadOnlyEnties(Status status) {
	if ( status==Status.DELETED || status==Status.MANAGED || status==Status.SAVING ) {
		hasNonReadOnlyEntities = true;
	}
}
 
Example 18
Source File: OptimisticLockingBidirectionalChildUpdatesRootVersionTest.java    From high-performance-java-persistence with Apache License 2.0 4 votes vote down vote up
private boolean deleted(FlushEntityEvent event) {
    return event.getEntityEntry().getStatus() == Status.DELETED;
}
 
Example 19
Source File: AbstractSaveEventListener.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Prepares the save call by checking the session caches for a pre-existing
 * entity and performing any lifecycle callbacks.
 *
 * @param entity The entity to be saved.
 * @param id The id by which to save the entity.
 * @param persister The entity's persister instance.
 * @param useIdentityColumn Is an identity column being used?
 * @param anything Generally cascade-specific information.
 * @param source The session from which the event originated.
 * @param requiresImmediateIdAccess does the event context require
 * access to the identifier immediately after execution of this method (if
 * not, post-insert style id generators may be postponed if we are outside
 * a transaction).
 *
 * @return The id used to save the entity; may be null depending on the
 *         type of id generator used and the requiresImmediateIdAccess value
 */
protected Serializable performSave(
		Object entity,
		Serializable id,
		EntityPersister persister,
		boolean useIdentityColumn,
		Object anything,
		EventSource source,
		boolean requiresImmediateIdAccess) {

	if ( LOG.isTraceEnabled() ) {
		LOG.tracev( "Saving {0}", MessageHelper.infoString( persister, id, source.getFactory() ) );
	}

	final EntityKey key;
	if ( !useIdentityColumn ) {
		key = source.generateEntityKey( id, persister );
		Object old = source.getPersistenceContext().getEntity( key );
		if ( old != null ) {
			if ( source.getPersistenceContext().getEntry( old ).getStatus() == Status.DELETED ) {
				source.forceFlush( source.getPersistenceContext().getEntry( old ) );
			}
			else {
				throw new NonUniqueObjectException( id, persister.getEntityName() );
			}
		}
		persister.setIdentifier( entity, id, source );
	}
	else {
		key = null;
	}

	if ( invokeSaveLifecycle( entity, persister, source ) ) {
		return id; //EARLY EXIT
	}

	return performSaveOrReplicate(
			entity,
			key,
			persister,
			useIdentityColumn,
			anything,
			source,
			requiresImmediateIdAccess
	);
}
 
Example 20
Source File: AbstractReactiveSaveEventListener.java    From hibernate-reactive with GNU Lesser General Public License v2.1 4 votes vote down vote up
/**
 * Prepares the save call by checking the session caches for a pre-existing
 * entity and performing any lifecycle callbacks.
 *
 * @param entity The entity to be saved.
 * @param id The id by which to save the entity.
 * @param persister The entity's persister instance.
 * @param useIdentityColumn Is an identity column being used?
 * @param context Generally cascade-specific information.
 * @param source The session from which the event originated.
 * @param requiresImmediateIdAccess does the event context require
 * access to the identifier immediately after execution of this method (if
 * not, post-insert style id generators may be postponed if we are outside
 * a transaction).
 *
 * @return The id used to save the entity; may be null depending on the
 * type of id generator used and the requiresImmediateIdAccess value
 */
protected CompletionStage<Void> reactivePerformSave(
		Object entity,
		Serializable id,
		EntityPersister persister,
		boolean useIdentityColumn,
		C context,
		EventSource source,
		boolean requiresImmediateIdAccess) {

	if ( LOG.isTraceEnabled() ) {
		LOG.tracev( "Saving {0}", MessageHelper.infoString( persister, id, source.getFactory() ) );
	}

	final EntityKey key;
	if ( !useIdentityColumn ) {
		key = source.generateEntityKey( id, persister );
		final PersistenceContext persistenceContext = source.getPersistenceContextInternal();
		Object old = persistenceContext.getEntity( key );
		if ( old != null ) {
			if ( persistenceContext.getEntry( old ).getStatus() == Status.DELETED ) {
				source.forceFlush( persistenceContext.getEntry( old ) );
			}
			else {
				return CompletionStages.failedFuture( new NonUniqueObjectException( id, persister.getEntityName() ) );
			}
		}
		persister.setIdentifier( entity, id, source );
	}
	else {
		key = null;
	}

	return reactivePerformSaveOrReplicate(
			entity,
			key,
			persister,
			useIdentityColumn,
			context,
			source,
			requiresImmediateIdAccess
	);
}