Java Code Examples for org.hibernate.engine.spi.EntityEntry#getLoadedState()

The following examples show how to use org.hibernate.engine.spi.EntityEntry#getLoadedState() . 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: 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 2
Source File: AbstractEntityPersister.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
private Object initializeLazyPropertiesFromCache(
		final String fieldName,
		final Object entity,
		final SharedSessionContractImplementor session,
		final EntityEntry entry,
		final CacheEntry cacheEntry) {

	LOG.trace( "Initializing lazy properties from second-level cache" );

	Object result = null;
	Serializable[] disassembledValues = cacheEntry.getDisassembledState();
	final Object[] snapshot = entry.getLoadedState();
	for ( int j = 0; j < lazyPropertyNames.length; j++ ) {
		final Serializable cachedValue = disassembledValues[lazyPropertyNumbers[j]];
		final Type lazyPropertyType = lazyPropertyTypes[j];
		final String propertyName = lazyPropertyNames[j];
		if (cachedValue == LazyPropertyInitializer.UNFETCHED_PROPERTY) {
			if (fieldName.equals(propertyName)) {
				result = LazyPropertyInitializer.UNFETCHED_PROPERTY;
			}
			// don't try to initialize the unfetched property
		}
		else {
			final Object propValue = lazyPropertyType.assemble(
					cachedValue,
					session,
					entity
			);
			if ( initializeLazyProperty( fieldName, entity, session, snapshot, j, propValue ) ) {
				result = propValue;
			}
		}
	}

	LOG.trace( "Done initializing lazy properties" );

	return result;
}
 
Example 3
Source File: AbstractEntityPersister.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Delete an object
 */
public void delete(Serializable id, Object version, Object object, SharedSessionContractImplementor session)
		throws HibernateException {
	final int span = getTableSpan();
	boolean isImpliedOptimisticLocking = !entityMetamodel.isVersioned() && isAllOrDirtyOptLocking();
	Object[] loadedState = null;
	if ( isImpliedOptimisticLocking ) {
		// need to treat this as if it where optimistic-lock="all" (dirty does *not* make sense);
		// first we need to locate the "loaded" state
		//
		// Note, it potentially could be a proxy, so doAfterTransactionCompletion the location the safe way...
		final EntityKey key = session.generateEntityKey( id, this );
		Object entity = session.getPersistenceContext().getEntity( key );
		if ( entity != null ) {
			EntityEntry entry = session.getPersistenceContext().getEntry( entity );
			loadedState = entry.getLoadedState();
		}
	}

	final String[] deleteStrings;
	if ( isImpliedOptimisticLocking && loadedState != null ) {
		// we need to utilize dynamic delete statements
		deleteStrings = generateSQLDeletStrings( loadedState );
	}
	else {
		// otherwise, utilize the static delete statements
		deleteStrings = getSQLDeleteStrings();
	}

	for ( int j = span - 1; j >= 0; j-- ) {
		delete( id, version, j, object, deleteStrings[j], session, loadedState );
	}

}
 
Example 4
Source File: AbstractEntityInsertAction.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void afterDeserialize(SharedSessionContractImplementor session) {
	super.afterDeserialize( session );
	// IMPL NOTE: non-flushed changes code calls this method with session == null...
	// guard against NullPointerException
	if ( session != null ) {
		final EntityEntry entityEntry = session.getPersistenceContext().getEntry( getInstance() );
		this.state = entityEntry.getLoadedState();
	}
}
 
Example 5
Source File: ReactiveAbstractEntityPersister.java    From hibernate-reactive with GNU Lesser General Public License v2.1 4 votes vote down vote up
default CompletionStage<?> deleteReactive(
		Serializable id, Object version, Object object,
		SharedSessionContractImplementor session)
		throws HibernateException {
	final int span = delegate().getTableSpan();
	boolean isImpliedOptimisticLocking = !delegate().getEntityMetamodel().isVersioned() && isAllOrDirtyOptimisticLocking();
	Object[] loadedState = null;
	if ( isImpliedOptimisticLocking ) {
		// need to treat this as if it where optimistic-lock="all" (dirty does *not* make sense);
		// first we need to locate the "loaded" state
		//
		// Note, it potentially could be a proxy, so doAfterTransactionCompletion the location the safe way...
		final EntityKey key = session.generateEntityKey( id, delegate());
		final PersistenceContext persistenceContext = session.getPersistenceContextInternal();
		Object entity = persistenceContext.getEntity( key );
		if ( entity != null ) {
			EntityEntry entry = persistenceContext.getEntry( entity );
			loadedState = entry.getLoadedState();
		}
	}

	final String[] deleteStrings;
	if ( isImpliedOptimisticLocking && loadedState != null ) {
		// we need to utilize dynamic delete statements
		deleteStrings = generateSQLDeleteStrings( loadedState );
	}
	else {
		// otherwise, utilize the static delete statements
		deleteStrings = delegate().getSQLDeleteStrings();
	}

	CompletionStage<?> deleteStage = CompletionStages.nullFuture();
	for ( int j = span - 1; j >= 0; j-- ) {
		// For now we assume there is only one delete query
		int jj = j;
		Object[] state = loadedState;
		deleteStage = deleteStage.thenCompose(
				v-> deleteReactive(
						id,
						version,
						jj,
						object,
						deleteStrings[jj],
						session,
						state
				));
	}

	return deleteStage;
}
 
Example 6
Source File: DefaultDeleteEventListener.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Perform the entity deletion.  Well, as with most operations, does not
 * really perform it; just schedules an action/execution with the
 * {@link org.hibernate.engine.spi.ActionQueue} for execution during flush.
 *
 * @param session The originating session
 * @param entity The entity to delete
 * @param entityEntry The entity's entry in the {@link PersistenceContext}
 * @param isCascadeDeleteEnabled Is delete cascading enabled?
 * @param persister The entity persister.
 * @param transientEntities A cache of already deleted entities.
 */
protected final void deleteEntity(
		final EventSource session,
		final Object entity,
		final EntityEntry entityEntry,
		final boolean isCascadeDeleteEnabled,
		final boolean isOrphanRemovalBeforeUpdates,
		final EntityPersister persister,
		final Set transientEntities) {

	if ( LOG.isTraceEnabled() ) {
		LOG.tracev(
				"Deleting {0}",
				MessageHelper.infoString( persister, entityEntry.getId(), session.getFactory() )
		);
	}

	final PersistenceContext persistenceContext = session.getPersistenceContext();
	final Type[] propTypes = persister.getPropertyTypes();
	final Object version = entityEntry.getVersion();

	final Object[] currentState;
	if ( entityEntry.getLoadedState() == null ) {
		//ie. the entity came in from update()
		currentState = persister.getPropertyValues( entity );
	}
	else {
		currentState = entityEntry.getLoadedState();
	}

	final Object[] deletedState = createDeletedState( persister, currentState, session );
	entityEntry.setDeletedState( deletedState );

	session.getInterceptor().onDelete(
			entity,
			entityEntry.getId(),
			deletedState,
			persister.getPropertyNames(),
			propTypes
	);

	// before any callbacks, etc, so subdeletions see that this deletion happened first
	persistenceContext.setEntryStatus( entityEntry, Status.DELETED );
	final EntityKey key = session.generateEntityKey( entityEntry.getId(), persister );

	cascadeBeforeDelete( session, persister, entity, entityEntry, transientEntities );

	new ForeignKeys.Nullifier( entity, true, false, session )
			.nullifyTransientReferences( entityEntry.getDeletedState(), propTypes );
	new Nullability( session ).checkNullability( entityEntry.getDeletedState(), persister, Nullability.NullabilityCheckType.DELETE );
	persistenceContext.getNullifiableEntityKeys().add( key );

	if ( isOrphanRemovalBeforeUpdates ) {
		// TODO: The removeOrphan concept is a temporary "hack" for HHH-6484.  This should be removed once action/task
		// ordering is improved.
		session.getActionQueue().addAction(
				new OrphanRemovalAction(
						entityEntry.getId(),
						deletedState,
						version,
						entity,
						persister,
						isCascadeDeleteEnabled,
						session
				)
		);
	}
	else {
		// Ensures that containing deletions happen before sub-deletions
		session.getActionQueue().addAction(
				new EntityDeleteAction(
						entityEntry.getId(),
						deletedState,
						version,
						entity,
						persister,
						isCascadeDeleteEnabled,
						session
				)
		);
	}

	cascadeAfterDelete( session, persister, entity, transientEntities );

	// the entry will be removed after the flush, and will no longer
	// override the stale snapshot
	// This is now handled by removeEntity() in EntityDeleteAction
	//persistenceContext.removeDatabaseSnapshot(key);
}
 
Example 7
Source File: CollectionType.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Get the key value from the owning entity instance, usually the identifier, but might be some
 * other unique key, in the case of property-ref
 *
 * @param owner The collection owner
 * @param session The session from which the request is originating.
 * @return The collection owner's key
 */
public Serializable getKeyOfOwner(Object owner, SharedSessionContractImplementor session) {

	EntityEntry entityEntry = session.getPersistenceContext().getEntry( owner );
	if ( entityEntry == null ) {
		// This just handles a particular case of component
		// projection, perhaps get rid of it and throw an exception
		return null;
	}

	if ( foreignKeyPropertyName == null ) {
		return entityEntry.getId();
	}
	else {
		// TODO: at the point where we are resolving collection references, we don't
		// know if the uk value has been resolved (depends if it was earlier or
		// later in the mapping document) - now, we could try and use e.getStatus()
		// to decide to semiResolve(), trouble is that initializeEntity() reuses
		// the same array for resolved and hydrated values
		Object id;
		if ( entityEntry.getLoadedState() != null ) {
			id = entityEntry.getLoadedValue( foreignKeyPropertyName );
		}
		else {
			id = entityEntry.getPersister().getPropertyValue( owner, foreignKeyPropertyName );
		}

		// NOTE VERY HACKISH WORKAROUND!!
		// TODO: Fix this so it will work for non-POJO entity mode
		Type keyType = getPersister( session ).getKeyType();
		Class returnedClass = keyType.getReturnedClass();

		if ( !returnedClass.isInstance( id ) ) {
			id = keyType.semiResolve(
					entityEntry.getLoadedValue( foreignKeyPropertyName ),
					session,
					owner
			);
		}

		return (Serializable) id;
	}
}