Java Code Examples for org.hibernate.persister.collection.CollectionPersister#getRole()

The following examples show how to use org.hibernate.persister.collection.CollectionPersister#getRole() . 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: BatchingCollectionInitializer.java    From webdsl with Apache License 2.0 6 votes vote down vote up
protected static boolean isCached(
		PersistenceContext context,
		Serializable collectionKey,
		CollectionPersister persister,
		EntityMode entityMode) {
	if ( persister.hasCache() ) {
		CacheKey cacheKey = new CacheKey(
				collectionKey,
		        persister.getKeyType(),
		        persister.getRole(),
		        entityMode,
		        context.getSession().getFactory()
		);
		return persister.getCacheAccessStrategy().get( cacheKey, context.getSession().getTimestamp() ) != null;
	}
	return false;
}
 
Example 2
Source File: BatchFetchQueue.java    From cacheonix-core with GNU Lesser General Public License v2.1 6 votes vote down vote up
private boolean isCached(
		Serializable collectionKey,
		CollectionPersister persister,
		EntityMode entityMode) {
	if ( persister.hasCache() ) {
		CacheKey cacheKey = new CacheKey(
				collectionKey,
		        persister.getKeyType(),
		        persister.getRole(),
		        entityMode,
		        context.getSession().getFactory()
		);
		return persister.getCache().getCache().get( cacheKey ) != null;
	}
	return false;
}
 
Example 3
Source File: CollectionAction.java    From cacheonix-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
public CollectionAction(
		final CollectionPersister persister, 
		final PersistentCollection collection, 
		final Serializable key, 
		final SessionImplementor session)
throws CacheException {
	this.persister = persister;
	this.session = session;
	this.key = key;
	this.collectionRole = persister.getRole();
	this.collection = collection;
}
 
Example 4
Source File: SessionFactoryImpl.java    From cacheonix-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
public void evictCollection(String roleName, Serializable id) throws HibernateException {
	CollectionPersister p = getCollectionPersister(roleName);
	if ( p.hasCache() ) {
		if ( log.isDebugEnabled() ) {
			log.debug( "evicting second-level cache: " + MessageHelper.collectionInfoString(p, id, this) );
		}
		CacheKey cacheKey = new CacheKey( id, p.getKeyType(), p.getRole(), EntityMode.POJO, this );
		p.getCache().remove( cacheKey );
	}
}
 
Example 5
Source File: CollectionStatisticsImpl.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
CollectionStatisticsImpl(CollectionPersister persister) {
	super(
			() -> persister.getCacheAccessStrategy() != null
					? persister.getCacheAccessStrategy().getRegion()
					: null
	);

	this.collectionRole = persister.getRole();
}
 
Example 6
Source File: CollectionAction.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
protected CollectionAction(
		final CollectionPersister persister,
		final PersistentCollection collection, 
		final Serializable key, 
		final SharedSessionContractImplementor session) {
	this.persister = persister;
	this.session = session;
	this.key = key;
	this.collectionRole = persister.getRole();
	this.collection = collection;
}
 
Example 7
Source File: CollectionKey.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
public CollectionKey(CollectionPersister persister, Serializable key) {
	this(
			persister.getRole(),
			key,
			persister.getKeyType(),
			persister.getOwnerEntityPersister().getEntityMetamodel().getEntityMode(),
			persister.getFactory()
	);
}
 
Example 8
Source File: CollectionKey.java    From cacheonix-core with GNU Lesser General Public License v2.1 4 votes vote down vote up
public CollectionKey(CollectionPersister persister, Serializable key, EntityMode em) {
	this( persister.getRole(), key, persister.getKeyType(), em, persister.getFactory() );
}
 
Example 9
Source File: DefaultInitializeCollectionEventListener.java    From cacheonix-core with GNU Lesser General Public License v2.1 4 votes vote down vote up
/**
 * Try to initialize a collection from the cache
 */
private boolean initializeCollectionFromCache(
		Serializable id,
		CollectionPersister persister,
		PersistentCollection collection,
		SessionImplementor source)
throws HibernateException {

	if ( !source.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;
	}
	else {
		
		final SessionFactoryImplementor factory = source.getFactory();

		final CacheKey ck = new CacheKey( 
				id, 
				persister.getKeyType(), 
				persister.getRole(), 
				source.getEntityMode(), 
				source.getFactory() 
			);
		Object ce = persister.getCache().get( ck, source.getTimestamp() );
		
		if ( factory.getStatistics().isStatisticsEnabled() ) {
			if (ce==null) {
				factory.getStatisticsImplementor().secondLevelCacheMiss( 
						persister.getCache().getRegionName() 
					);
			}
			else {
				factory.getStatisticsImplementor().secondLevelCacheHit( 
						persister.getCache().getRegionName() 
					);
			}

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

			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;
		}
		
	}
}
 
Example 10
Source File: CollectionLoadContext.java    From cacheonix-core with GNU Lesser General Public License v2.1 4 votes vote down vote up
/**
 * Add the collection to the second-level cache
 *
 * @param lce The entry representing the collection to add
 * @param persister The persister
 */
private void addCollectionToCache(LoadingCollectionEntry lce, CollectionPersister persister) {
	final SessionImplementor session = getLoadContext().getPersistenceContext().getSession();
	final SessionFactoryImplementor factory = session.getFactory();

	if ( log.isDebugEnabled() ) {
		log.debug( "Caching collection: " + MessageHelper.collectionInfoString( persister, lce.getKey(), factory ) );
	}

	if ( !session.getEnabledFilters().isEmpty() && persister.isAffectedByEnabledFilters( session ) ) {
		// some filters affecting the collection are enabled on the session, so do not do the put into the cache.
		log.debug( "Refusing to add to cache due to enabled filters" );
		// todo : add the notion of enabled filters to the CacheKey to differentiate filtered collections from non-filtered;
		//      but CacheKey is currently used for both collections and entities; would ideally need to define two seperate ones;
		//      currently this works in conjuction with the check on
		//      DefaultInitializeCollectionEventHandler.initializeCollectionFromCache() (which makes sure to not read from
		//      cache with enabled filters).
		return; // EARLY EXIT!!!!!
	}

	final Comparator versionComparator;
	final Object version;
	if ( persister.isVersioned() ) {
		versionComparator = persister.getOwnerEntityPersister().getVersionType().getComparator();
		final Object collectionOwner = getLoadContext().getPersistenceContext().getCollectionOwner( lce.getKey(), persister );
		version = getLoadContext().getPersistenceContext().getEntry( collectionOwner ).getVersion();
	}
	else {
		version = null;
		versionComparator = null;
	}

	CollectionCacheEntry entry = new CollectionCacheEntry( lce.getCollection(), persister );
	CacheKey cacheKey = new CacheKey(
			lce.getKey(),
			persister.getKeyType(),
			persister.getRole(),
			session.getEntityMode(),
			session.getFactory()
	);
	boolean put = persister.getCache().put(
			cacheKey,
			persister.getCacheEntryStructure().structure(entry),
			session.getTimestamp(),
			version,
			versionComparator,
			factory.getSettings().isMinimalPutsEnabled() && session.getCacheMode()!= CacheMode.REFRESH
	);

	if ( put && factory.getStatistics().isStatisticsEnabled() ) {
		factory.getStatisticsImplementor().secondLevelCachePut( persister.getCache().getRegionName() );
	}
}
 
Example 11
Source File: Collections.java    From cacheonix-core with GNU Lesser General Public License v2.1 4 votes vote down vote up
/**
 * 1. record the collection role that this collection is referenced by
 * 2. decide if the collection needs deleting/creating/updating (but
 *	don't actually schedule the action yet)
 */
private static void prepareCollectionForUpdate(
		PersistentCollection collection,
        CollectionEntry entry,
        EntityMode entityMode,
        SessionFactoryImplementor factory)
throws HibernateException {

	if ( entry.isProcessed() ) {
		throw new AssertionFailure( "collection was processed twice by flush()" );
	}
	entry.setProcessed(true);

	final CollectionPersister loadedPersister = entry.getLoadedPersister();
	final CollectionPersister currentPersister = entry.getCurrentPersister();
	if ( loadedPersister != null || currentPersister != null ) {					// it is or was referenced _somewhere_

		boolean ownerChanged = loadedPersister != currentPersister ||				// if either its role changed,
		                       !currentPersister
				                       .getKeyType().isEqual(                       // or its key changed
												entry.getLoadedKey(),
		                                        entry.getCurrentKey(),
		                                        entityMode, factory
		                       );

		if (ownerChanged) {

			// do a check
			final boolean orphanDeleteAndRoleChanged = loadedPersister != null &&
			                                           currentPersister != null &&
			                                           loadedPersister.hasOrphanDelete();

			if (orphanDeleteAndRoleChanged) {
				throw new HibernateException(
						"Don't change the reference to a collection with cascade=\"all-delete-orphan\": " +
						loadedPersister.getRole()
					);
			}

			// do the work
			if ( currentPersister != null ) {
				entry.setDorecreate(true);											// we will need to create new entries
			}

			if ( loadedPersister != null ) {
				entry.setDoremove(true);											// we will need to remove ye olde entries
				if ( entry.isDorecreate() ) {
					log.trace( "Forcing collection initialization" );
					collection.forceInitialization();								// force initialize!
				}
			}

		}
		else if ( collection.isDirty() ) {											// else if it's elements changed
			entry.setDoupdate(true);
		}

	}

}
 
Example 12
Source File: Collections.java    From cacheonix-core with GNU Lesser General Public License v2.1 4 votes vote down vote up
private static void processDereferencedCollection(PersistentCollection coll, SessionImplementor session)
throws HibernateException {

	final PersistenceContext persistenceContext = session.getPersistenceContext();
	CollectionEntry entry = persistenceContext.getCollectionEntry(coll);
	final CollectionPersister loadedPersister = entry.getLoadedPersister();

	if ( log.isDebugEnabled() && loadedPersister != null )
		log.debug(
				"Collection dereferenced: " +
				MessageHelper.collectionInfoString(
						loadedPersister,
				        entry.getLoadedKey(),
				        session.getFactory()
					)
			);

	// do a check
	boolean hasOrphanDelete = loadedPersister != null &&
	                          loadedPersister.hasOrphanDelete();
	if (hasOrphanDelete) {
		Serializable ownerId = loadedPersister.getOwnerEntityPersister()
				.getIdentifier( coll.getOwner(), session.getEntityMode() );
		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().getSettings().isIdentifierRollbackEnabled() ) {
				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" );
			}
		}
		EntityKey key = new EntityKey(
				ownerId,
		        loadedPersister.getOwnerEntityPersister(),
		        session.getEntityMode()
		);
		Object owner = persistenceContext.getEntity(key);
		if ( owner == null ) {
			throw new AssertionFailure(
					"collection owner not associated with session: " +
					loadedPersister.getRole()
			);
		}
		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.getEntityMode(), session.getFactory() );

}
 
Example 13
Source File: DefaultCacheKeysFactory.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
public static Object staticCreateCollectionKey(Object id, CollectionPersister persister, SessionFactoryImplementor factory, String tenantIdentifier) {
	return new CacheKeyImplementation( id, persister.getKeyType(), persister.getRole(), tenantIdentifier, factory );
}
 
Example 14
Source File: Collections.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
/**
 * 1. record the collection role that this collection is referenced by
 * 2. decide if the collection needs deleting/creating/updating (but
 *	don't actually schedule the action yet)
 */
@SuppressWarnings( {"JavaDoc"})
private static void prepareCollectionForUpdate(
		PersistentCollection collection,
		CollectionEntry entry,
		SessionFactoryImplementor factory) {
	if ( entry.isProcessed() ) {
		throw new AssertionFailure( "collection was processed twice by flush()" );
	}
	entry.setProcessed( true );

	final CollectionPersister loadedPersister = entry.getLoadedPersister();
	final CollectionPersister currentPersister = entry.getCurrentPersister();
	if ( loadedPersister != null || currentPersister != null ) {
		// it is or was referenced _somewhere_

		// check if the key changed
		// excludes marking key changed when the loaded key is a DelayedPostInsertIdentifier.
		final boolean keyChanged = currentPersister != null
				&& entry != null
				&& !currentPersister.getKeyType().isEqual( entry.getLoadedKey(), entry.getCurrentKey(), factory )
				&& !( entry.getLoadedKey() instanceof DelayedPostInsertIdentifier );

		// if either its role changed, or its key changed
		final boolean ownerChanged = loadedPersister != currentPersister || keyChanged;

		if ( ownerChanged ) {
			// do a check
			final boolean orphanDeleteAndRoleChanged =
					loadedPersister != null && currentPersister != null && loadedPersister.hasOrphanDelete();

			if (orphanDeleteAndRoleChanged) {
				throw new HibernateException(
						"Don't change the reference to a collection with delete-orphan enabled : "
								+ loadedPersister.getRole()
				);
			}

			// do the work
			if ( currentPersister != null ) {
				entry.setDorecreate( true );
			}

			if ( loadedPersister != null ) {
				// we will need to remove ye olde entries
				entry.setDoremove( true );
				if ( entry.isDorecreate() ) {
					LOG.trace( "Forcing collection initialization" );
					collection.forceInitialization();
				}
			}
		}
		else if ( collection.isDirty() ) {
			// the collection's elements have changed
			entry.setDoupdate( true );
		}
	}
}
 
Example 15
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 16
Source File: CollectionKey.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
public CollectionKey(CollectionPersister persister, Serializable key, EntityMode em) {
	this( persister.getRole(), key, persister.getKeyType(), em, persister.getFactory() );
}
 
Example 17
Source File: HibernateKeyWrapper.java    From ignite with Apache License 2.0 2 votes vote down vote up
/**
 * @param id ID.
 * @param persister Persister.
 * @param tenantIdentifier Tenant ID.
 * @return Cache key.
 * @see DefaultCacheKeysFactory#staticCreateCollectionKey(Object, CollectionPersister, SessionFactoryImplementor, String)
 */
static Object staticCreateCollectionKey(Object id,
    CollectionPersister persister,
    String tenantIdentifier) {
    return new HibernateKeyWrapper(id, persister.getRole(), tenantIdentifier);
}
 
Example 18
Source File: HibernateKeyWrapper.java    From ignite with Apache License 2.0 2 votes vote down vote up
/**
 * @param id ID.
 * @param persister Persister.
 * @param tenantIdentifier Tenant ID.
 * @return Cache key.
 * @see DefaultCacheKeysFactory#staticCreateCollectionKey(Object, CollectionPersister, SessionFactoryImplementor, String)
 */
static Object staticCreateCollectionKey(Object id,
    CollectionPersister persister,
    String tenantIdentifier) {
    return new HibernateKeyWrapper(id, persister.getRole(), tenantIdentifier);
}