Java Code Examples for org.hibernate.cache.spi.access.NaturalIdDataAccess#generateCacheKey()

The following examples show how to use org.hibernate.cache.spi.access.NaturalIdDataAccess#generateCacheKey() . 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: NaturalIdXrefDelegate.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Handle removing cross reference entries for the given natural-id/pk combo
 *
 * @param persister The persister representing the entity type.
 * @param pk The primary key value
 * @param naturalIdValues The natural id value(s)
 * 
 * @return The cached values, if any.  May be different from incoming values.
 */
public Object[] removeNaturalIdCrossReference(EntityPersister persister, Serializable pk, Object[] naturalIdValues) {
	persister = locatePersisterForKey( persister );
	validateNaturalId( persister, naturalIdValues );

	final NaturalIdResolutionCache entityNaturalIdResolutionCache = naturalIdResolutionCacheMap.get( persister );
	Object[] sessionCachedNaturalIdValues = null;
	if ( entityNaturalIdResolutionCache != null ) {
		final CachedNaturalId cachedNaturalId = entityNaturalIdResolutionCache.pkToNaturalIdMap
				.remove( pk );
		if ( cachedNaturalId != null ) {
			entityNaturalIdResolutionCache.naturalIdToPkMap.remove( cachedNaturalId );
			sessionCachedNaturalIdValues = cachedNaturalId.getValues();
		}
	}

	if ( persister.hasNaturalIdCache() ) {
		final NaturalIdDataAccess naturalIdCacheAccessStrategy = persister
				.getNaturalIdCacheAccessStrategy();
		final Object naturalIdCacheKey = naturalIdCacheAccessStrategy.generateCacheKey( naturalIdValues, persister, session() );
		naturalIdCacheAccessStrategy.evict( naturalIdCacheKey );

		if ( sessionCachedNaturalIdValues != null
				&& !Arrays.equals( sessionCachedNaturalIdValues, naturalIdValues ) ) {
			final Object sessionNaturalIdCacheKey = naturalIdCacheAccessStrategy.generateCacheKey( sessionCachedNaturalIdValues, persister, session() );
			naturalIdCacheAccessStrategy.evict( sessionNaturalIdCacheKey );
		}
	}

	return sessionCachedNaturalIdValues;
}
 
Example 2
Source File: StatefulPersistenceContext.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
		public void removeSharedNaturalIdCrossReference(EntityPersister persister, Serializable id, Object[] naturalIdValues) {
			if ( !persister.hasNaturalIdentifier() ) {
				// nothing to do
				return;
			}

			if ( ! persister.hasNaturalIdCache() ) {
				// nothing to do
				return;
			}

			// todo : couple of things wrong here:
			//		1) should be using access strategy, not plain evict..
			//		2) should prefer session-cached values if any (requires interaction from removeLocalNaturalIdCrossReference

			persister = locateProperPersister( persister );
			final NaturalIdDataAccess naturalIdCacheAccessStrategy = persister.getNaturalIdCacheAccessStrategy();
			final Object naturalIdCacheKey = naturalIdCacheAccessStrategy.generateCacheKey( naturalIdValues, persister, session );
			naturalIdCacheAccessStrategy.evict( naturalIdCacheKey );

//			if ( sessionCachedNaturalIdValues != null
//					&& !Arrays.equals( sessionCachedNaturalIdValues, deletedNaturalIdValues ) ) {
//				final NaturalIdCacheKey sessionNaturalIdCacheKey = new NaturalIdCacheKey( sessionCachedNaturalIdValues, persister, session );
//				naturalIdCacheAccessStrategy.evict( sessionNaturalIdCacheKey );
//			}
		}
 
Example 3
Source File: NaturalIdXrefDelegate.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Given a persister and natural-id value(s), find the locally cross-referenced primary key.  Will return
 * {@link PersistenceContext.NaturalIdHelper#INVALID_NATURAL_ID_REFERENCE} if the given natural ids are known to
 * be invalid (see {@link #stashInvalidNaturalIdReference}).
 *
 * @param persister The persister representing the entity type.
 * @param naturalIdValues The natural id value(s)
 * 
 * @return The corresponding cross-referenced primary key, 
 * 		{@link PersistenceContext.NaturalIdHelper#INVALID_NATURAL_ID_REFERENCE},
 * 		or {@code null} if none 
 */
public Serializable findCachedNaturalIdResolution(EntityPersister persister, Object[] naturalIdValues) {
	persister = locatePersisterForKey( persister );
	validateNaturalId( persister, naturalIdValues );

	NaturalIdResolutionCache entityNaturalIdResolutionCache = naturalIdResolutionCacheMap.get( persister );

	Serializable pk;
	final CachedNaturalId cachedNaturalId = new CachedNaturalId( persister, naturalIdValues );
	if ( entityNaturalIdResolutionCache != null ) {
		pk = entityNaturalIdResolutionCache.naturalIdToPkMap.get( cachedNaturalId );

		// Found in session cache
		if ( pk != null ) {
			if ( LOG.isTraceEnabled() ) {
				LOG.trace(
						"Resolved natural key -> primary key resolution in session cache: " +
								persister.getRootEntityName() + "#[" +
								Arrays.toString( naturalIdValues ) + "]"
				);
			}

			return pk;
		}

		// if we did not find a hit, see if we know about these natural ids as invalid...
		if ( entityNaturalIdResolutionCache.containsInvalidNaturalIdReference( naturalIdValues ) ) {
			return PersistenceContext.NaturalIdHelper.INVALID_NATURAL_ID_REFERENCE;
		}
	}

	// Session cache miss, see if second-level caching is enabled
	if ( !persister.hasNaturalIdCache() ) {
		return null;
	}

	// Try resolution from second-level cache
	final NaturalIdDataAccess naturalIdCacheAccessStrategy = persister.getNaturalIdCacheAccessStrategy();
	final Object naturalIdCacheKey = naturalIdCacheAccessStrategy.generateCacheKey( naturalIdValues, persister, session() );

	pk = CacheHelper.fromSharedCache( session(), naturalIdCacheKey, naturalIdCacheAccessStrategy );

	// Found in second-level cache, store in session cache
	final SessionFactoryImplementor factory = session().getFactory();
	if ( pk != null ) {
		if ( factory.getStatistics().isStatisticsEnabled() ) {
			factory.getStatistics().naturalIdCacheHit(
					StatsHelper.INSTANCE.getRootEntityRole( persister ),
					naturalIdCacheAccessStrategy.getRegion().getName()
			);
		}

		if ( LOG.isTraceEnabled() ) {
			// protected to avoid Arrays.toString call unless needed
			LOG.tracef(
					"Found natural key [%s] -> primary key [%s] xref in second-level cache for %s",
					Arrays.toString( naturalIdValues ),
					pk,
					persister.getRootEntityName()
			);
		}

		if ( entityNaturalIdResolutionCache == null ) {
			entityNaturalIdResolutionCache = new NaturalIdResolutionCache( persister );
			NaturalIdResolutionCache existingCache = naturalIdResolutionCacheMap.putIfAbsent( persister, entityNaturalIdResolutionCache );
			if ( existingCache != null ) {
				entityNaturalIdResolutionCache = existingCache;
			}
		}

		entityNaturalIdResolutionCache.pkToNaturalIdMap.put( pk, cachedNaturalId );
		entityNaturalIdResolutionCache.naturalIdToPkMap.put( cachedNaturalId, pk );
	}
	else if ( factory.getStatistics().isStatisticsEnabled() ) {
		factory.getStatistics().naturalIdCacheMiss(
				StatsHelper.INSTANCE.getRootEntityRole( persister ),
				naturalIdCacheAccessStrategy.getRegion().getName()
		);
	}

	return pk;
}