Java Code Examples for org.hibernate.event.spi.EventSource#getPersistenceContextInternal()

The following examples show how to use org.hibernate.event.spi.EventSource#getPersistenceContextInternal() . 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: DefaultReactiveLockEventListener.java    From hibernate-reactive with GNU Lesser General Public License v2.1 6 votes vote down vote up
private void cascadeOnLock(LockEvent event, EntityPersister persister, Object entity) {
	EventSource source = event.getSession();
	final PersistenceContext persistenceContext = source.getPersistenceContextInternal();
	persistenceContext.incrementCascadeLevel();
	try {
		new Cascade(
				CascadingActions.LOCK,
				CascadePoint.AFTER_LOCK,
				persister,
				entity,
				event.getLockOptions(),
				source
		).cascade();
	}
	finally {
		persistenceContext.decrementCascadeLevel();
	}
}
 
Example 2
Source File: DefaultReactiveAutoFlushEventListener.java    From hibernate-reactive with GNU Lesser General Public License v2.1 5 votes vote down vote up
private boolean flushMightBeNeeded(final EventSource source) {
	final PersistenceContext persistenceContext = source.getPersistenceContextInternal();
	return !source.getHibernateFlushMode().lessThan( FlushMode.AUTO )
			&& source.getDontFlushFromFind() == 0
			&& ( persistenceContext.getNumberOfManagedEntities() > 0 ||
			persistenceContext.getCollectionEntriesSize() > 0 );
}
 
Example 3
Source File: DefaultReactiveLoadEventListener.java    From hibernate-reactive with GNU Lesser General Public License v2.1 5 votes vote down vote up
private void cacheNaturalId(LoadEvent event, EntityPersister persister, EventSource session, Object entity) {
	if ( entity != null && persister.hasNaturalIdentifier() ) {
		final PersistenceContext persistenceContext = session.getPersistenceContextInternal();
		final PersistenceContext.NaturalIdHelper naturalIdHelper = persistenceContext.getNaturalIdHelper();
		naturalIdHelper.cacheNaturalIdCrossReferenceFromLoad(
				persister,
				event.getEntityId(),
				naturalIdHelper.extractNaturalIdValues(
						entity,
						persister
				)
		);
	}
}
 
Example 4
Source File: AbstractReactiveFlushingEventListener.java    From hibernate-reactive with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
	 * Coordinates the processing necessary to get things ready for executions
	 * as db calls by preping the session caches and moving the appropriate
	 * entities and collections to their respective execution queues.
	 *
	 * @param event The flush event.
	 * @throws HibernateException Error flushing caches to execution queues.
	 */
	protected CompletionStage<Void> flushEverythingToExecutions(FlushEvent event) throws HibernateException {

		LOG.trace( "Flushing session" );

		EventSource session = event.getSession();

		final PersistenceContext persistenceContext = session.getPersistenceContextInternal();
		session.getInterceptor().preFlush( persistenceContext.managedEntitiesIterator() );

		CompletionStage<Void> cascades = prepareEntityFlushes(session, persistenceContext);
		// we could move this inside if we wanted to
		// tolerate collection initializations during
		// collection dirty checking:
		prepareCollectionFlushes( persistenceContext );
		// now, any collections that are initialized
		// inside this block do not get updated - they
		// are ignored until the next flush

		return cascades.thenAccept( v -> {
			persistenceContext.setFlushing(true);
			try {
				int entityCount = flushEntities(event, persistenceContext);
				int collectionCount = flushCollections(session, persistenceContext);

				event.setNumberOfEntitiesProcessed(entityCount);
				event.setNumberOfCollectionsProcessed(collectionCount);
			}
			finally {
				persistenceContext.setFlushing(false);
			}
		});

		//some statistics
//		logFlushResults( event );
	}
 
Example 5
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
	);
}