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

The following are Jave code examples for showing how to use generateCacheKey() of the org.hibernate.engine.spi.SessionImplementor class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: lams   File: AbstractEntityPersister.java   View Source Code Vote up 5 votes
public Object initializeLazyProperty(String fieldName, Object entity, SessionImplementor session)
		throws HibernateException {

	final Serializable id = session.getContextEntityIdentifier( entity );

	final EntityEntry entry = session.getPersistenceContext().getEntry( entity );
	if ( entry == null ) {
		throw new HibernateException( "entity is not associated with the session: " + id );
	}

	if ( LOG.isTraceEnabled() ) {
		LOG.tracev( "Initializing lazy properties of: {0}, field access: {1}", MessageHelper.infoString( this, id, getFactory() ), fieldName );
	}

	if ( session.getCacheMode().isGetEnabled() && hasCache() ) {
		final CacheKey cacheKey = session.generateCacheKey( id, getIdentifierType(), getEntityName() );
		final Object ce = CacheHelper.fromSharedCache( session, cacheKey, getCacheAccessStrategy() );
		if ( ce != null ) {
			final CacheEntry cacheEntry = (CacheEntry) getCacheEntryStructure().destructure(ce, factory);
			if ( !cacheEntry.areLazyPropertiesUnfetched() ) {
				//note early exit here:
				return initializeLazyPropertiesFromCache( fieldName, entity, session, entry, cacheEntry );
			}
		}
	}

	return initializeLazyPropertiesFromDatastore( fieldName, entity, session, id, entry );

}
 
Example 2
Project: lams   File: DefaultLoadEventListener.java   View Source Code Vote up 5 votes
/**
 * If the class to be loaded has been configured with a cache, then lock
 * given id in that cache and then perform the load.
 *
 * @param event The initiating load request event
 * @param persister The persister corresponding to the entity to be loaded
 * @param keyToLoad The key of the entity to be loaded
 * @param options The defined load options
 * @param source The originating session
 *
 * @return The loaded entity
 *
 * @throws HibernateException
 */
protected Object lockAndLoad(
		final LoadEvent event,
		final EntityPersister persister,
		final EntityKey keyToLoad,
		final LoadEventListener.LoadType options,
		final SessionImplementor source) {
	SoftLock lock = null;
	final CacheKey ck;
	if ( persister.hasCache() ) {
		ck = source.generateCacheKey(
				event.getEntityId(),
				persister.getIdentifierType(),
				persister.getRootEntityName()
		);
		lock = persister.getCacheAccessStrategy().lockItem( ck, null );
	}
	else {
		ck = null;
	}

	Object entity;
	try {
		entity = load( event, persister, keyToLoad, options );
	}
	finally {
		if ( persister.hasCache() ) {
			persister.getCacheAccessStrategy().unlockItem( ck, lock );
		}
	}

	return event.getSession().getPersistenceContext().proxyFor( persister, keyToLoad, entity );
}
 
Example 3
Project: lams   File: CollectionAction.java   View Source Code Vote up 5 votes
@Override
public void doAfterTransactionCompletion(boolean success, SessionImplementor session) {
	final CacheKey ck = session.generateCacheKey(
			key,
			persister.getKeyType(),
			persister.getRole()
	);
	persister.getCacheAccessStrategy().unlockItem( ck, lock );
}
 
Example 4
Project: lams   File: DefaultInitializeCollectionEventListener.java   View Source Code Vote up 4 votes
/**
 * 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 CacheKey ck = source.generateCacheKey( id, persister.getKeyType(), persister.getRole() );
	final Object ce = CacheHelper.fromSharedCache( source, ck, persister.getCacheAccessStrategy() );

	if ( factory.getStatistics().isStatisticsEnabled() ) {
		if ( ce == null ) {
			factory.getStatisticsImplementor()
					.secondLevelCacheMiss( persister.getCacheAccessStrategy().getRegion().getName() );
		}
		else {
			factory.getStatisticsImplementor()
					.secondLevelCacheHit( persister.getCacheAccessStrategy().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;
}
 
Example 5
Project: lams   File: DefaultLoadEventListener.java   View Source Code Vote up 4 votes
/**
 * Attempts to load the entity from the second-level cache.
 *
 * @param event The load event
 * @param persister The persister for the entity being requested for load
 * @param options The load options.
 *
 * @return The entity from the second-level cache, or null.
 */
protected Object loadFromSecondLevelCache(
		final LoadEvent event,
		final EntityPersister persister,
		final LoadEventListener.LoadType options) {

	final SessionImplementor source = event.getSession();
	final boolean useCache = persister.hasCache()
			&& source.getCacheMode().isGetEnabled()
			&& event.getLockMode().lessThan( LockMode.READ );

	if ( !useCache ) {
		// we can't use cache here
		return null;
	}

	final SessionFactoryImplementor factory = source.getFactory();
	final CacheKey ck = source.generateCacheKey(
			event.getEntityId(),
			persister.getIdentifierType(),
			persister.getRootEntityName()
	);

	final Object ce = CacheHelper.fromSharedCache( source, ck, persister.getCacheAccessStrategy() );
	if ( factory.getStatistics().isStatisticsEnabled() ) {
		if ( ce == null ) {
			factory.getStatisticsImplementor().secondLevelCacheMiss(
					persister.getCacheAccessStrategy().getRegion().getName()
			);
		}
		else {
			factory.getStatisticsImplementor().secondLevelCacheHit(
					persister.getCacheAccessStrategy().getRegion().getName()
			);
		}
	}

	if ( ce == null ) {
		// nothing was found in cache
		return null;
	}

	CacheEntry entry = (CacheEntry) persister.getCacheEntryStructure().destructure( ce, factory );
	Object entity = convertCacheEntryToEntity( entry, event.getEntityId(), persister, event );
	
	if ( !persister.isInstance( entity ) ) {
		throw new WrongClassException(
				"loaded object was of wrong class " + entity.getClass(),
				event.getEntityId(),
				persister.getEntityName()
			);
	}
	
	return entity;
}
 
Example 6
Project: lams   File: EntityDeleteAction.java   View Source Code Vote up 4 votes
@Override
public void execute() throws HibernateException {
	final Serializable id = getId();
	final EntityPersister persister = getPersister();
	final SessionImplementor session = getSession();
	final Object instance = getInstance();

	final boolean veto = preDelete();

	Object version = this.version;
	if ( persister.isVersionPropertyGenerated() ) {
		// we need to grab the version value from the entity, otherwise
		// we have issues with generated-version entities that may have
		// multiple actions queued during the same flush
		version = persister.getVersion( instance );
	}

	final CacheKey ck;
	if ( persister.hasCache() ) {
		ck = session.generateCacheKey( id, persister.getIdentifierType(), persister.getRootEntityName() );
		lock = persister.getCacheAccessStrategy().lockItem( ck, version );
	}
	else {
		ck = null;
	}

	if ( !isCascadeDeleteEnabled && !veto ) {
		persister.delete( id, version, instance, session );
	}
	
	//postDelete:
	// After actually deleting a row, record the fact that the instance no longer 
	// exists on the database (needed for identity-column key generation), and
	// remove it from the session cache
	final PersistenceContext persistenceContext = session.getPersistenceContext();
	final EntityEntry entry = persistenceContext.removeEntry( instance );
	if ( entry == null ) {
		throw new AssertionFailure( "possible nonthreadsafe access to session" );
	}
	entry.postDelete();

	persistenceContext.removeEntity( entry.getEntityKey() );
	persistenceContext.removeProxy( entry.getEntityKey() );
	
	if ( persister.hasCache() ) {
		persister.getCacheAccessStrategy().remove( ck );
	}

	persistenceContext.getNaturalIdHelper().removeSharedNaturalIdCrossReference( persister, id, naturalIdValues );

	postDelete();

	if ( getSession().getFactory().getStatistics().isStatisticsEnabled() && !veto ) {
		getSession().getFactory().getStatisticsImplementor().deleteEntity( getPersister().getEntityName() );
	}
}
 
Example 7
Project: lams   File: EntityInsertAction.java   View Source Code Vote up 4 votes
@Override
public void execute() throws HibernateException {
	nullifyTransientReferencesIfNotAlready();

	final EntityPersister persister = getPersister();
	final SessionImplementor session = getSession();
	final Object instance = getInstance();
	final Serializable id = getId();

	final boolean veto = preInsert();

	// Don't need to lock the cache here, since if someone
	// else inserted the same pk first, the insert would fail

	if ( !veto ) {
		
		persister.insert( id, getState(), instance, session );

		final EntityEntry entry = session.getPersistenceContext().getEntry( instance );
		if ( entry == null ) {
			throw new AssertionFailure( "possible non-threadsafe access to session" );
		}
		
		entry.postInsert( getState() );

		if ( persister.hasInsertGeneratedProperties() ) {
			persister.processInsertGeneratedProperties( id, instance, getState(), session );
			if ( persister.isVersionPropertyGenerated() ) {
				version = Versioning.getVersion( getState(), persister );
			}
			entry.postUpdate( instance, getState(), version );
		}

		getSession().getPersistenceContext().registerInsertedKey( getPersister(), getId() );
	}

	final SessionFactoryImplementor factory = getSession().getFactory();

	if ( isCachePutEnabled( persister, session ) ) {
		final CacheEntry ce = persister.buildCacheEntry(
				instance,
				getState(),
				version,
				session
		);
		cacheEntry = persister.getCacheEntryStructure().structure( ce );
		final CacheKey ck = session.generateCacheKey( id, persister.getIdentifierType(), persister.getRootEntityName() );

		final boolean put = cacheInsert( persister, ck );

		if ( put && factory.getStatistics().isStatisticsEnabled() ) {
			factory.getStatisticsImplementor().secondLevelCachePut( getPersister().getCacheAccessStrategy().getRegion().getName() );
		}
	}

	handleNaturalIdPostSaveNotifications( id );

	postInsert();

	if ( factory.getStatistics().isStatisticsEnabled() && !veto ) {
		factory.getStatisticsImplementor().insertEntity( getPersister().getEntityName() );
	}

	markExecuted();
}