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

The following examples show how to use org.hibernate.engine.spi.SessionImplementor#getPersistenceContext() . 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: Collections.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
private static void processNeverReferencedCollection(PersistentCollection coll, SessionImplementor session)
		throws HibernateException {
	final PersistenceContext persistenceContext = session.getPersistenceContext();
	final CollectionEntry entry = persistenceContext.getCollectionEntry( coll );

	if ( LOG.isDebugEnabled() ) {
		LOG.debugf(
				"Found collection with unloaded owner: %s",
				MessageHelper.collectionInfoString( 
						entry.getLoadedPersister(),
						coll,
						entry.getLoadedKey(),
						session
				)
		);
	}

	entry.setCurrentPersister( entry.getLoadedPersister() );
	entry.setCurrentKey( entry.getLoadedKey() );

	prepareCollectionForUpdate( coll, entry, session.getFactory() );

}
 
Example 2
Source File: AbstractFlushingEventListener.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * 1. Recreate the collection key -> collection map
 * 2. rebuild the collection entries
 * 3. call Interceptor.postFlush()
 */
protected void postFlush(SessionImplementor session) throws HibernateException {

	LOG.trace( "Post flush" );

	final PersistenceContext persistenceContext = session.getPersistenceContext();
	persistenceContext.getCollectionsByKey().clear();
	
	// the database has changed now, so the subselect results need to be invalidated
	// the batch fetching queues should also be cleared - especially the collection batch fetching one
	persistenceContext.getBatchFetchQueue().clear();

	for ( Map.Entry<PersistentCollection, CollectionEntry> me : IdentityMap.concurrentEntries( persistenceContext.getCollectionEntries() ) ) {
		CollectionEntry collectionEntry = me.getValue();
		PersistentCollection persistentCollection = me.getKey();
		collectionEntry.postFlush(persistentCollection);
		if ( collectionEntry.getLoadedPersister() == null ) {
			//if the collection is dereferenced, unset its session reference and remove from the session cache
			//iter.remove(); //does not work, since the entrySet is not backed by the set
			persistentCollection.unsetSession( session );
			persistenceContext.getCollectionEntries()
					.remove(persistentCollection);
		}
		else {
			//otherwise recreate the mapping between the collection and its key
			CollectionKey collectionKey = new CollectionKey(
					collectionEntry.getLoadedPersister(),
					collectionEntry.getLoadedKey()
			);
			persistenceContext.getCollectionsByKey().put(collectionKey, persistentCollection);
		}
	}

}
 
Example 3
Source File: Collections.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
private static void processDereferencedCollection(PersistentCollection coll, SessionImplementor session) {
	final PersistenceContext persistenceContext = session.getPersistenceContext();
	final CollectionEntry entry = persistenceContext.getCollectionEntry( coll );
	final CollectionPersister loadedPersister = entry.getLoadedPersister();

	if ( loadedPersister != null && LOG.isDebugEnabled() ) {
		LOG.debugf(
				"Collection dereferenced: %s",
				MessageHelper.collectionInfoString( loadedPersister, 
						coll, entry.getLoadedKey(), session
				)
		);
	}

	// do a check
	final boolean hasOrphanDelete = loadedPersister != null && loadedPersister.hasOrphanDelete();
	if ( hasOrphanDelete ) {
		Serializable ownerId = loadedPersister.getOwnerEntityPersister().getIdentifier( coll.getOwner(), session );
		if ( ownerId == null ) {
			// the owning entity may have been deleted and its identifier unset due to
			// identifier-rollback; in which case, try to look up its identifier from
			// the persistence context
			if ( session.getFactory().getSessionFactoryOptions().isIdentifierRollbackEnabled() ) {
				final EntityEntry ownerEntry = persistenceContext.getEntry( coll.getOwner() );
				if ( ownerEntry != null ) {
					ownerId = ownerEntry.getId();
				}
			}
			if ( ownerId == null ) {
				throw new AssertionFailure( "Unable to determine collection owner identifier for orphan-delete processing" );
			}
		}
		final EntityKey key = session.generateEntityKey( ownerId, loadedPersister.getOwnerEntityPersister() );
		final Object owner = persistenceContext.getEntity( key );
		if ( owner == null ) {
			throw new AssertionFailure(
					"collection owner not associated with session: " +
					loadedPersister.getRole()
			);
		}
		final EntityEntry e = persistenceContext.getEntry( owner );
		//only collections belonging to deleted entities are allowed to be dereferenced in the case of orphan delete
		if ( e != null && e.getStatus() != Status.DELETED && e.getStatus() != Status.GONE ) {
			throw new HibernateException(
					"A collection with cascade=\"all-delete-orphan\" was no longer referenced by the owning entity instance: " +
					loadedPersister.getRole()
			);
		}
	}

	// do the work
	entry.setCurrentPersister( null );
	entry.setCurrentKey( null );
	prepareCollectionForUpdate( coll, entry, session.getFactory() );

}
 
Example 4
Source File: WrapVisitor.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
final Object processArrayOrNewCollection(Object collection, CollectionType collectionType)
		throws HibernateException {

	final SessionImplementor session = getSession();

	if ( collection == null ) {
		//do nothing
		return null;
	}
	else {
		CollectionPersister persister = session.getFactory().getCollectionPersister( collectionType.getRole() );

		final PersistenceContext persistenceContext = session.getPersistenceContext();
		//TODO: move into collection type, so we can use polymorphism!
		if ( collectionType.hasHolder() ) {

			if ( collection == CollectionType.UNFETCHED_COLLECTION ) {
				return null;
			}

			PersistentCollection ah = persistenceContext.getCollectionHolder( collection );
			if ( ah == null ) {
				ah = collectionType.wrap( session, collection );
				persistenceContext.addNewCollection( persister, ah );
				persistenceContext.addCollectionHolder( ah );
			}
			return null;
		}
		else {

			PersistentCollection persistentCollection = collectionType.wrap( session, collection );
			persistenceContext.addNewCollection( persister, persistentCollection );

			if ( LOG.isTraceEnabled() ) {
				LOG.tracev( "Wrapped collection in role: {0}", collectionType.getRole() );
			}

			return persistentCollection; //Force a substitution!

		}

	}

}
 
Example 5
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;
}