Java Code Examples for org.hibernate.engine.spi.SessionImplementor#getFactory()

The following examples show how to use org.hibernate.engine.spi.SessionImplementor#getFactory() . 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: ReactiveDynamicBatchingEntityLoaderBuilder.java    From hibernate-reactive with GNU Lesser General Public License v2.1 6 votes vote down vote up
private CompletionStage<List<Object>> performOrderedBatchLoad(
		List<Serializable> idsInBatch,
		LockOptions lockOptions,
		OuterJoinLoadable persister,
		SessionImplementor session) {
	final int batchSize =  idsInBatch.size();
	final ReactiveDynamicBatchingEntityLoader batchingLoader = new ReactiveDynamicBatchingEntityLoader(
			persister,
			batchSize,
			lockOptions,
			session.getFactory(),
			session.getLoadQueryInfluencers()
	);

	final Serializable[] idsInBatchArray = idsInBatch.toArray(new Serializable[0]);
	QueryParameters qp = buildMultiLoadQueryParameters( persister, idsInBatchArray, lockOptions );
	CompletionStage<List<Object>> result = batchingLoader.doEntityBatchFetch(session, qp, idsInBatchArray);
	idsInBatch.clear();
	return result;
}
 
Example 2
Source File: DefaultReactiveInitializeCollectionEventListener.java    From hibernate-reactive with GNU Lesser General Public License v2.1 4 votes vote down vote up
/**
 * Try to initialize a collection from the cache
 *
 * @param id The id of the collection to initialize
 * @param persister The collection persister
 * @param collection The collection to initialize
 * @param source The originating session
 *
 * @return true if we were able to initialize the collection from the cache;
 *         false otherwise.
 */
private boolean initializeCollectionFromCache(
		Serializable id,
		CollectionPersister persister,
		PersistentCollection collection,
		SessionImplementor source) {

	if ( source.getLoadQueryInfluencers().hasEnabledFilters() && persister.isAffectedByEnabledFilters( source ) ) {
		LOG.trace( "Disregarding cached version (if any) of collection due to enabled filters" );
		return false;
	}

	final boolean useCache = persister.hasCache() && source.getCacheMode().isGetEnabled();

	if ( !useCache ) {
		return false;
	}

	final SessionFactoryImplementor factory = source.getFactory();
	final CollectionDataAccess cacheAccessStrategy = persister.getCacheAccessStrategy();
	final Object ck = cacheAccessStrategy.generateCacheKey( id, persister, factory, source.getTenantIdentifier() );
	final Object ce = CacheHelper.fromSharedCache( source, ck, cacheAccessStrategy );

	final StatisticsImplementor statistics = factory.getStatistics();
	if ( statistics.isStatisticsEnabled() ) {
		if ( ce == null ) {
			statistics.collectionCacheMiss( persister.getNavigableRole(), cacheAccessStrategy.getRegion().getName() );
		}
		else {
			statistics.collectionCacheHit( persister.getNavigableRole(), cacheAccessStrategy.getRegion().getName() );
		}
	}

	if ( ce == null ) {
		return false;
	}

	CollectionCacheEntry cacheEntry = (CollectionCacheEntry) persister.getCacheEntryStructure().destructure( ce, factory );

	final PersistenceContext persistenceContext = source.getPersistenceContextInternal();
	cacheEntry.assemble( collection, persister, persistenceContext.getCollectionOwner( id, persister ) );
	persistenceContext.getCollectionEntry( collection ).postInitialize( collection );
	return true;
}
 
Example 3
Source File: Collections.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
/**
    * Initialize the role of the collection.
    *
    * @param collection The collection to be updated by reachability.
    * @param type The type of the collection.
    * @param entity The owner of the collection.
 * @param session The session from which this request originates
    */
public static void processReachableCollection(
		PersistentCollection collection,
		CollectionType type,
		Object entity,
		SessionImplementor session) {
	collection.setOwner( entity );
	final CollectionEntry ce = session.getPersistenceContext().getCollectionEntry( collection );

	if ( ce == null ) {
		// refer to comment in StatefulPersistenceContext.addCollection()
		throw new HibernateException(
				"Found two representations of same collection: " +
				type.getRole()
		);
	}

	final SessionFactoryImplementor factory = session.getFactory();
	final CollectionPersister persister = factory.getMetamodel().collectionPersister( type.getRole() );

	ce.setCurrentPersister( persister );
	//TODO: better to pass the id in as an argument?
	ce.setCurrentKey( type.getKeyOfOwner( entity, session ) );

	final boolean isBytecodeEnhanced = persister.getOwnerEntityPersister().getInstrumentationMetadata().isEnhancedForLazyLoading();
	if ( isBytecodeEnhanced && !collection.wasInitialized() ) {
		// skip it
		LOG.debugf(
				"Skipping uninitialized bytecode-lazy collection: %s",
				MessageHelper.collectionInfoString( persister, collection, ce.getCurrentKey(), session )
		);
		ce.setReached( true );
		ce.setProcessed( true );
	}
	else {
		// The CollectionEntry.isReached() stuff is just to detect any silly users
		// who set up circular or shared references between/to collections.
		if ( ce.isReached() ) {
			// We've been here before
			throw new HibernateException(
					"Found shared references to a collection: " + type.getRole()
			);
		}
		ce.setReached( true );

		if ( LOG.isDebugEnabled() ) {
			if ( collection.wasInitialized() ) {
				LOG.debugf(
						"Collection found: %s, was: %s (initialized)",
						MessageHelper.collectionInfoString(
								persister,
								collection,
								ce.getCurrentKey(),
								session
						),
						MessageHelper.collectionInfoString(
								ce.getLoadedPersister(),
								collection,
								ce.getLoadedKey(),
								session
						)
				);
			}
			else {
				LOG.debugf(
						"Collection found: %s, was: %s (uninitialized)",
						MessageHelper.collectionInfoString(
								persister,
								collection,
								ce.getCurrentKey(),
								session
						),
						MessageHelper.collectionInfoString(
								ce.getLoadedPersister(),
								collection,
								ce.getLoadedKey(),
								session
						)
				);
			}
		}

		prepareCollectionForUpdate( collection, ce, factory );
	}
}
 
Example 4
Source File: DefaultInitializeCollectionEventListener.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Try to initialize a collection from the cache
 *
 * @param id The id of the collection of initialize
 * @param persister The collection persister
 * @param collection The collection to initialize
 * @param source The originating session
 *
 * @return true if we were able to initialize the collection from the cache;
 *         false otherwise.
 */
private boolean initializeCollectionFromCache(
		Serializable id,
		CollectionPersister persister,
		PersistentCollection collection,
		SessionImplementor source) {

	if ( !source.getLoadQueryInfluencers().getEnabledFilters().isEmpty()
			&& persister.isAffectedByEnabledFilters( source ) ) {
		LOG.trace( "Disregarding cached version (if any) of collection due to enabled filters" );
		return false;
	}

	final boolean useCache = persister.hasCache() && source.getCacheMode().isGetEnabled();

	if ( !useCache ) {
		return false;
	}

	final SessionFactoryImplementor factory = source.getFactory();
	final CollectionDataAccess cacheAccessStrategy = persister.getCacheAccessStrategy();
	final Object ck = cacheAccessStrategy.generateCacheKey( id, persister, factory, source.getTenantIdentifier() );
	final Object ce = CacheHelper.fromSharedCache( source, ck, persister.getCacheAccessStrategy() );

	if ( factory.getStatistics().isStatisticsEnabled() ) {
		if ( ce == null ) {
			factory.getStatistics().collectionCacheMiss(
					persister.getNavigableRole(),
					cacheAccessStrategy.getRegion().getName()
			);
		}
		else {
			factory.getStatistics().collectionCacheHit(
					persister.getNavigableRole(),
					cacheAccessStrategy.getRegion().getName()
			);
		}
	}

	if ( ce == null ) {
		return false;
	}

	CollectionCacheEntry cacheEntry = (CollectionCacheEntry) persister.getCacheEntryStructure().destructure(
			ce,
			factory
	);

	final PersistenceContext persistenceContext = source.getPersistenceContext();
	cacheEntry.assemble( collection, persister, persistenceContext.getCollectionOwner( id, persister ) );
	persistenceContext.getCollectionEntry( collection ).postInitialize( collection );
	// addInitializedCollection(collection, persister, id);
	return true;
}