org.hibernate.event.spi.EventSource#getPersistenceContextInternal ( )源码实例Demo

下面列出了org.hibernate.event.spi.EventSource#getPersistenceContextInternal ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

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();
	}
}
 
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 );
}
 
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
				)
		);
	}
}
 
/**
	 * 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 );
	}
 
/**
 * 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
	);
}