下面列出了org.hibernate.persister.collection.CollectionPersister#getKeyType ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
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;
}
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;
}
public CollectionKey(CollectionPersister persister, Serializable key) {
this(
persister.getRole(),
key,
persister.getKeyType(),
persister.getOwnerEntityPersister().getEntityMetamodel().getEntityMode(),
persister.getFactory()
);
}
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 );
}
}
public CollectionKey(CollectionPersister persister, Serializable key, EntityMode em) {
this( persister.getRole(), key, persister.getKeyType(), em, persister.getFactory() );
}
public static Object staticCreateCollectionKey(Object id, CollectionPersister persister, SessionFactoryImplementor factory, String tenantIdentifier) {
return new CacheKeyImplementation( id, persister.getKeyType(), persister.getRole(), tenantIdentifier, factory );
}
public CollectionKey(CollectionPersister persister, Serializable key, EntityMode em) {
this( persister.getRole(), key, persister.getKeyType(), em, persister.getFactory() );
}
/**
* 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() );
}
}
/**
* 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;
}
}
}