Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Centralize creation of EntityKey and CacheKey into AbstractSessionImp…

…l. This is in preparation for multi tenancy (NH-3087). Based on work by Paul White in pull request #91.
  • Loading branch information...
commit ce60683f571d0178a74ce008dcf1cd2bdc0401c1 1 parent 8ed010d
@oskarb oskarb authored
Showing with 109 additions and 79 deletions.
  1. +1 −1  src/NHibernate.DomainModel/CustomPersister.cs
  2. +3 −3 src/NHibernate/Action/CollectionAction.cs
  3. +1 −1  src/NHibernate/Action/CollectionUpdateAction.cs
  4. +3 −3 src/NHibernate/Action/EntityDeleteAction.cs
  5. +1 −1  src/NHibernate/Action/EntityIdentityInsertAction.cs
  6. +2 −2 src/NHibernate/Action/EntityInsertAction.cs
  7. +2 −2 src/NHibernate/Action/EntityUpdateAction.cs
  8. +10 −14 src/NHibernate/Engine/BatchFetchQueue.cs
  9. +1 −1  src/NHibernate/Engine/Collections.cs
  10. +1 −1  src/NHibernate/Engine/EntityEntry.cs
  11. +5 −0 src/NHibernate/Engine/ISessionImplementor.cs
  12. +1 −1  src/NHibernate/Engine/Loading/CollectionLoadContext.cs
  13. +5 −5 src/NHibernate/Engine/StatefulPersistenceContext.cs
  14. +1 −1  src/NHibernate/Engine/TwoPhaseLoad.cs
  15. +1 −1  src/NHibernate/Event/Default/AbstractLockUpgradeEventListener.cs
  16. +1 −1  src/NHibernate/Event/Default/AbstractReassociateEventListener.cs
  17. +2 −2 src/NHibernate/Event/Default/AbstractSaveEventListener.cs
  18. +2 −2 src/NHibernate/Event/Default/DefaultDeleteEventListener.cs
  19. +1 −1  src/NHibernate/Event/Default/DefaultEvictEventListener.cs
  20. +1 −1  src/NHibernate/Event/Default/DefaultFlushEntityEventListener.cs
  21. +1 −1  src/NHibernate/Event/Default/DefaultInitializeCollectionEventListener.cs
  22. +4 −4 src/NHibernate/Event/Default/DefaultLoadEventListener.cs
  23. +2 −2 src/NHibernate/Event/Default/DefaultMergeEventListener.cs
  24. +3 −3 src/NHibernate/Event/Default/DefaultRefreshEventListener.cs
  25. +3 −3 src/NHibernate/Event/Default/DefaultReplicateEventListener.cs
  26. +1 −1  src/NHibernate/Event/Default/DefaultSaveOrUpdateEventListener.cs
  27. +16 −0 src/NHibernate/Impl/AbstractSessionImpl.cs
  28. +17 −3 src/NHibernate/Impl/SessionFactoryImpl.cs
  29. +2 −2 src/NHibernate/Impl/StatelessSessionImpl.cs
  30. +1 −2  src/NHibernate/Loader/Collection/BatchingCollectionInitializer.cs
  31. +1 −1  src/NHibernate/Loader/Entity/BatchingEntityLoader.cs
  32. +2 −2 src/NHibernate/Loader/Loader.cs
  33. +1 −1  src/NHibernate/Persister/Collection/AbstractCollectionPersister.cs
  34. +3 −3 src/NHibernate/Persister/Entity/AbstractEntityPersister.cs
  35. +1 −1  src/NHibernate/Persister/Entity/NamedQueryLoader.cs
  36. +2 −2 src/NHibernate/Proxy/AbstractLazyInitializer.cs
  37. +1 −1  src/NHibernate/Proxy/Poco/BasicLazyInitializer.cs
  38. +1 −1  src/NHibernate/Type/ManyToOneType.cs
  39. +2 −2 src/NHibernate/Type/OneToOneType.cs
View
2  src/NHibernate.DomainModel/CustomPersister.cs
@@ -317,7 +317,7 @@ public object Load(object id, object optionalObject, LockMode lockMode, ISession
if (obj != null)
{
clone = (Custom)obj.Clone();
- TwoPhaseLoad.AddUninitializedEntity(new EntityKey(id, this, session.EntityMode), clone, this, LockMode.None, false,
+ TwoPhaseLoad.AddUninitializedEntity(session.GenerateEntityKey(id, this), clone, this, LockMode.None, false,
session);
TwoPhaseLoad.PostHydrate(this, id, new String[] {obj.Name}, null, clone, LockMode.None, false, session);
TwoPhaseLoad.InitializeEntity(clone, false, session, new PreLoadEvent((IEventSource) session),
View
6 src/NHibernate/Action/CollectionAction.cs
@@ -96,7 +96,7 @@ public virtual void BeforeExecutions()
// second-level cache invalidation only)
if (persister.HasCache)
{
- CacheKey ck = new CacheKey(key, persister.KeyType, persister.Role, session.EntityMode, session.Factory);
+ CacheKey ck = session.GenerateCacheKey(key, persister.KeyType, persister.Role);
softLock = persister.Cache.Lock(ck, null);
}
}
@@ -120,7 +120,7 @@ public virtual AfterTransactionCompletionProcessDelegate AfterTransactionComplet
{
if (persister.HasCache)
{
- CacheKey ck = new CacheKey(key, persister.KeyType, persister.Role, Session.EntityMode, Session.Factory);
+ CacheKey ck = Session.GenerateCacheKey(key, persister.KeyType, persister.Role);
persister.Cache.Release(ck, softLock);
}
});
@@ -138,7 +138,7 @@ protected internal void Evict()
{
if (persister.HasCache)
{
- CacheKey ck = new CacheKey(key, persister.KeyType, persister.Role, session.EntityMode, session.Factory);
+ CacheKey ck = session.GenerateCacheKey(key, persister.KeyType, persister.Role);
persister.Cache.Evict(ck);
}
}
View
2  src/NHibernate/Action/CollectionUpdateAction.cs
@@ -130,7 +130,7 @@ public override AfterTransactionCompletionProcessDelegate AfterTransactionComple
// NH Different behavior: to support unlocking collections from the cache.(r3260)
if (Persister.HasCache)
{
- CacheKey ck = new CacheKey(Key, Persister.KeyType, Persister.Role, Session.EntityMode, Session.Factory);
+ CacheKey ck = Session.GenerateCacheKey(Key, Persister.KeyType, Persister.Role);
if (success)
{
View
6 src/NHibernate/Action/EntityDeleteAction.cs
@@ -57,7 +57,7 @@ public override void Execute()
CacheKey ck;
if (persister.HasCache)
{
- ck = new CacheKey(id, persister.IdentifierType, persister.RootEntityName, session.EntityMode, session.Factory);
+ ck = session.GenerateCacheKey(id, persister.IdentifierType, persister.RootEntityName);
sLock = persister.Cache.Lock(ck, version);
}
else
@@ -83,7 +83,7 @@ public override void Execute()
}
entry.PostDelete();
- EntityKey key = new EntityKey(entry.Id, entry.Persister, session.EntityMode);
+ EntityKey key = session.GenerateEntityKey(entry.Id, entry.Persister);
persistenceContext.RemoveEntity(key);
persistenceContext.RemoveProxy(key);
@@ -131,7 +131,7 @@ protected override void AfterTransactionCompletionProcessImpl(bool success)
{
if (Persister.HasCache)
{
- CacheKey ck = new CacheKey(Id, Persister.IdentifierType, Persister.RootEntityName, Session.EntityMode, Session.Factory);
+ CacheKey ck = Session.GenerateCacheKey(Id, Persister.IdentifierType, Persister.RootEntityName);
Persister.Cache.Release(ck, sLock);
}
if (success)
View
2  src/NHibernate/Action/EntityIdentityInsertAction.cs
@@ -41,7 +41,7 @@ private EntityKey GenerateDelayedEntityKey()
if (!isDelayed)
throw new HibernateException("Cannot request delayed entity-key for non-delayed post-insert-id generation");
- return new EntityKey(new DelayedPostInsertIdentifier(), Persister, Session.EntityMode);
+ return Session.GenerateEntityKey(new DelayedPostInsertIdentifier(), Persister);
}
}
View
4 src/NHibernate/Action/EntityInsertAction.cs
@@ -84,7 +84,7 @@ public override void Execute()
CacheEntry ce = new CacheEntry(state, persister, persister.HasUninitializedLazyProperties(instance, session.EntityMode), version, session, instance);
cacheEntry = persister.CacheEntryStructure.Structure(ce);
- CacheKey ck = new CacheKey(id, persister.IdentifierType, persister.RootEntityName, Session.EntityMode, Session.Factory);
+ CacheKey ck = Session.GenerateCacheKey(id, persister.IdentifierType, persister.RootEntityName);
bool put = persister.Cache.Insert(ck, cacheEntry, version);
if (put && factory.Statistics.IsStatisticsEnabled)
@@ -108,7 +108,7 @@ protected override void AfterTransactionCompletionProcessImpl(bool success)
IEntityPersister persister = Persister;
if (success && IsCachePutEnabled(persister))
{
- CacheKey ck = new CacheKey(Id, persister.IdentifierType, persister.RootEntityName, Session.EntityMode, Session.Factory);
+ CacheKey ck = Session.GenerateCacheKey(Id, persister.IdentifierType, persister.RootEntityName);
bool put = persister.Cache.AfterInsert(ck, cacheEntry, version);
if (put && Session.Factory.Statistics.IsStatisticsEnabled)
View
4 src/NHibernate/Action/EntityUpdateAction.cs
@@ -70,7 +70,7 @@ public override void Execute()
CacheKey ck = null;
if (persister.HasCache)
{
- ck = new CacheKey(id, persister.IdentifierType, persister.RootEntityName, session.EntityMode, factory);
+ ck = session.GenerateCacheKey(id, persister.IdentifierType, persister.RootEntityName);
slock = persister.Cache.Lock(ck, previousVersion);
}
@@ -140,7 +140,7 @@ protected override void AfterTransactionCompletionProcessImpl(bool success)
IEntityPersister persister = Persister;
if (persister.HasCache)
{
- CacheKey ck = new CacheKey(Id, persister.IdentifierType, persister.RootEntityName, Session.EntityMode, Session.Factory);
+ CacheKey ck = Session.GenerateCacheKey(Id, persister.IdentifierType, persister.RootEntityName);
if (success && cacheEntry != null)
{
View
24 src/NHibernate/Engine/BatchFetchQueue.cs
@@ -136,7 +136,7 @@ public void RemoveBatchLoadableEntityKey(EntityKey key)
/// <param name="batchSize">the maximum number of keys to return</param>
/// <param name="entityMode">The entity mode.</param>
/// <returns>an array of collection keys, of length batchSize (padded with nulls)</returns>
- public object[] GetCollectionBatch(ICollectionPersister collectionPersister, object id, int batchSize, EntityMode entityMode)
+ public object[] GetCollectionBatch(ICollectionPersister collectionPersister, object id, int batchSize)
{
object[] keys = new object[batchSize];
keys[0] = id;
@@ -160,14 +160,14 @@ public object[] GetCollectionBatch(ICollectionPersister collectionPersister, obj
//if ( end == -1 && count > batchSize*10 ) return keys; //try out ten batches, max
- bool isEqual = collectionPersister.KeyType.IsEqual(id, ce.LoadedKey, entityMode, collectionPersister.Factory);
+ bool isEqual = collectionPersister.KeyType.IsEqual(id, ce.LoadedKey, context.Session.EntityMode, collectionPersister.Factory);
if (isEqual)
{
end = i;
//checkForEnd = false;
}
- else if (!IsCached(ce.LoadedKey, collectionPersister, entityMode))
+ else if (!IsCached(ce.LoadedKey, collectionPersister))
{
keys[i++] = ce.LoadedKey;
//count++;
@@ -194,9 +194,8 @@ public object[] GetCollectionBatch(ICollectionPersister collectionPersister, obj
/// <param name="persister">The persister for the entities being loaded.</param>
/// <param name="id">The identifier of the entity currently demanding load.</param>
/// <param name="batchSize">The maximum number of keys to return</param>
- /// <param name="entityMode">The entity mode.</param>
/// <returns>an array of identifiers, of length batchSize (possibly padded with nulls)</returns>
- public object[] GetEntityBatch(IEntityPersister persister,object id,int batchSize, EntityMode entityMode)
+ public object[] GetEntityBatch(IEntityPersister persister,object id,int batchSize)
{
object[] ids = new object[batchSize];
ids[0] = id; //first element of array is reserved for the actual instance we are loading!
@@ -214,13 +213,13 @@ public object[] GetEntityBatch(IEntityPersister persister,object id,int batchSiz
//the first id found after the given id
return ids;
}
- if (persister.IdentifierType.IsEqual(id, key.Identifier, entityMode))
+ if (persister.IdentifierType.IsEqual(id, key.Identifier, context.Session.EntityMode))
{
end = i;
}
else
{
- if (!IsCached(key, persister, entityMode))
+ if (!IsCached(key, persister))
{
ids[i++] = key.Identifier;
}
@@ -236,24 +235,21 @@ public object[] GetEntityBatch(IEntityPersister persister,object id,int batchSiz
return ids; //we ran out of ids to try
}
- private bool IsCached(EntityKey entityKey, IEntityPersister persister, EntityMode entityMode)
+ private bool IsCached(EntityKey entityKey, IEntityPersister persister)
{
if (persister.HasCache)
{
- CacheKey key =
- new CacheKey(entityKey.Identifier, persister.IdentifierType, entityKey.EntityName, entityMode,
- context.Session.Factory);
+ CacheKey key = context.Session.GenerateCacheKey(entityKey.Identifier, persister.IdentifierType, entityKey.EntityName);
return persister.Cache.Cache.Get(key) != null;
}
return false;
}
- private bool IsCached(object collectionKey, ICollectionPersister persister, EntityMode entityMode)
+ private bool IsCached(object collectionKey, ICollectionPersister persister)
{
if (persister.HasCache)
{
- CacheKey cacheKey =
- new CacheKey(collectionKey, persister.KeyType, persister.Role, entityMode, context.Session.Factory);
+ CacheKey cacheKey = context.Session.GenerateCacheKey(collectionKey, persister.KeyType, persister.Role);
return persister.Cache.Cache.Get(cacheKey) != null;
}
return false;
View
2  src/NHibernate/Engine/Collections.cs
@@ -60,7 +60,7 @@ private static void ProcessDereferencedCollection(IPersistentCollection coll, IS
// throw new AssertionFailure("Unable to determine collection owner identifier for orphan-delete processing");
// }
//}
- EntityKey key = new EntityKey(ownerId, loadedPersister.OwnerEntityPersister, session.EntityMode);
+ EntityKey key = session.GenerateEntityKey(ownerId, loadedPersister.OwnerEntityPersister);
object owner = persistenceContext.GetEntity(key);
if (owner == null)
{
View
2  src/NHibernate/Engine/EntityEntry.cs
@@ -260,7 +260,7 @@ public void ForceLocked(object entity, object nextVersion)
public bool IsNullifiable(bool earlyInsert, ISessionImplementor session)
{
- return Status == Status.Saving || (earlyInsert ? !ExistsInDatabase : session.PersistenceContext.NullifiableEntityKeys.Contains(new EntityKey(Id, Persister, entityMode)));
+ return Status == Status.Saving || (earlyInsert ? !ExistsInDatabase : session.PersistenceContext.NullifiableEntityKeys.Contains(session.GenerateEntityKey(Id, Persister)));
}
public bool RequiresDirtyCheck(object entity)
View
5 src/NHibernate/Engine/ISessionImplementor.cs
@@ -3,6 +3,7 @@
using System.Collections.Generic;
using System.Data;
using NHibernate.AdoNet;
+using NHibernate.Cache;
using NHibernate.Collection;
using NHibernate.Engine.Query.Sql;
using NHibernate.Event;
@@ -303,5 +304,9 @@ public interface ISessionImplementor
ITransactionContext TransactionContext { get; set; }
void CloseSessionFromDistributedTransaction();
+
+ EntityKey GenerateEntityKey(object id, IEntityPersister persister);
+
+ CacheKey GenerateCacheKey(object id, IType type, string entityOrRoleName);
}
}
View
2  src/NHibernate/Engine/Loading/CollectionLoadContext.cs
@@ -323,7 +323,7 @@ private void AddCollectionToCache(LoadingCollectionEntry lce, ICollectionPersist
}
CollectionCacheEntry entry = new CollectionCacheEntry(lce.Collection, persister);
- CacheKey cacheKey = new CacheKey(lce.Key, persister.KeyType, persister.Role, session.EntityMode, factory);
+ CacheKey cacheKey = session.GenerateCacheKey(lce.Key, persister.KeyType, persister.Role);
bool put = persister.Cache.Put(cacheKey, persister.CacheEntryStructure.Structure(entry),
session.Timestamp, version, versionComparator,
factory.Settings.IsMinimalPutsEnabled && session.CacheMode != CacheMode.Refresh);
View
10 src/NHibernate/Engine/StatefulPersistenceContext.cs
@@ -330,7 +330,7 @@ public void AfterTransactionCompletion()
/// </summary>
public object[] GetDatabaseSnapshot(object id, IEntityPersister persister)
{
- EntityKey key = new EntityKey(id, persister, session.EntityMode);
+ EntityKey key = session.GenerateEntityKey(id, persister);
object cached;
if (entitySnapshotsByKey.TryGetValue(key, out cached))
{
@@ -614,7 +614,7 @@ private void ReassociateProxy(ILazyInitializer li, INHibernateProxy proxy)
if (li.Session != Session)
{
IEntityPersister persister = session.Factory.GetEntityPersister(li.EntityName);
- EntityKey key = new EntityKey(li.Identifier, persister, session.EntityMode);
+ EntityKey key = session.GenerateEntityKey(li.Identifier, persister);
// any earlier proxy takes precedence
if (!proxiesByKey.ContainsKey(key))
{
@@ -777,13 +777,13 @@ public object ProxyFor(object impl)
{
EntityEntry e = GetEntry(impl);
IEntityPersister p = e.Persister;
- return ProxyFor(p, new EntityKey(e.Id, p, session.EntityMode), impl);
+ return ProxyFor(p, session.GenerateEntityKey(e.Id, p), impl);
}
/// <summary> Get the entity that owns this persistent collection</summary>
public object GetCollectionOwner(object key, ICollectionPersister collectionPersister)
{
- return GetEntity(new EntityKey(key, collectionPersister.OwnerEntityPersister, session.EntityMode));
+ return GetEntity(session.GenerateEntityKey(key, collectionPersister.OwnerEntityPersister));
}
/// <summary> Get the entity that owned this persistent collection when it was loaded </summary>
@@ -1351,7 +1351,7 @@ public void ReplaceDelayedEntityIdentityInsertKeys(EntityKey oldKey, object gene
var oldEntry = (EntityEntry) tempObject2;
parentsByChild.Clear();
- var newKey = new EntityKey(generatedId, oldEntry.Persister, Session.EntityMode);
+ var newKey = Session.GenerateEntityKey(generatedId, oldEntry.Persister);
AddEntity(newKey, entity);
AddEntry(entity, oldEntry.Status, oldEntry.LoadedState, oldEntry.RowId, generatedId, oldEntry.Version,
oldEntry.LockMode, oldEntry.ExistsInDatabase, oldEntry.Persister, oldEntry.IsBeingReplicated,
View
2  src/NHibernate/Engine/TwoPhaseLoad.cs
@@ -106,7 +106,7 @@ public static void InitializeEntity(object entity, bool readOnly, ISessionImplem
object version = Versioning.GetVersion(hydratedState, persister);
CacheEntry entry =
new CacheEntry(hydratedState, persister, entityEntry.LoadedWithLazyPropertiesUnfetched, version, session, entity);
- CacheKey cacheKey = new CacheKey(id, persister.IdentifierType, persister.RootEntityName, session.EntityMode, session.Factory);
+ CacheKey cacheKey = session.GenerateCacheKey(id, persister.IdentifierType, persister.RootEntityName);
bool put =
persister.Cache.Put(cacheKey, persister.CacheEntryStructure.Structure(entry), session.Timestamp, version,
persister.IsVersioned ? persister.VersionType.Comparator : null,
View
2  src/NHibernate/Event/Default/AbstractLockUpgradeEventListener.cs
@@ -47,7 +47,7 @@ protected virtual void UpgradeLock(object entity, EntityEntry entry, LockMode re
CacheKey ck;
if (persister.HasCache)
{
- ck = new CacheKey(entry.Id, persister.IdentifierType, persister.RootEntityName, source.EntityMode, source.Factory);
+ ck = source.GenerateCacheKey(entry.Id, persister.IdentifierType, persister.RootEntityName);
slock = persister.Cache.Lock(ck, entry.Version);
}
else
View
2  src/NHibernate/Event/Default/AbstractReassociateEventListener.cs
@@ -33,7 +33,7 @@ protected EntityEntry Reassociate(AbstractEvent @event, object entity, object id
}
IEventSource source = @event.Session;
- EntityKey key = new EntityKey(id, persister, source.EntityMode);
+ EntityKey key = source.GenerateEntityKey(id, persister);
source.PersistenceContext.CheckUniqueness(key, entity);
View
4 src/NHibernate/Event/Default/AbstractSaveEventListener.cs
@@ -160,7 +160,7 @@ protected virtual object PerformSave(object entity, object id, IEntityPersister
EntityKey key;
if (!useIdentityColumn)
{
- key = new EntityKey(id, persister, source.EntityMode);
+ key = source.GenerateEntityKey(id, persister);
object old = source.PersistenceContext.GetEntity(key);
if (old != null)
{
@@ -260,7 +260,7 @@ protected virtual object PerformSaveOrReplicate(object entity, EntityKey key, IE
id = insert.GeneratedId;
//now done in EntityIdentityInsertAction
//persister.setIdentifier( entity, id, source.getEntityMode() );
- key = new EntityKey(id, persister, source.EntityMode);
+ key = source.GenerateEntityKey(id, persister);
source.PersistenceContext.CheckUniqueness(key, entity);
//source.getBatcher().executeBatch(); //found another way to ensure that all batched joined inserts have been executed
}
View
4 src/NHibernate/Event/Default/DefaultDeleteEventListener.cs
@@ -64,7 +64,7 @@ public virtual void OnDelete(DeleteEvent @event, ISet<object> transientEntities)
throw new TransientObjectException("the detached instance passed to delete() had a null identifier");
}
- EntityKey key = new EntityKey(id, persister, source.EntityMode);
+ EntityKey key = source.GenerateEntityKey(id, persister);
persistenceContext.CheckUniqueness(key, entity);
@@ -196,7 +196,7 @@ protected virtual void DeleteEntity(IEventSource session, object entity, EntityE
// before any callbacks, etc, so subdeletions see that this deletion happened first
persistenceContext.SetEntryStatus(entityEntry, Status.Deleted);
- EntityKey key = new EntityKey(entityEntry.Id, persister, session.EntityMode);
+ EntityKey key = session.GenerateEntityKey(entityEntry.Id, persister);
CascadeBeforeDelete(session, persister, entity, entityEntry, transientEntities);
View
2  src/NHibernate/Event/Default/DefaultEvictEventListener.cs
@@ -33,7 +33,7 @@ public virtual void OnEvict(EvictEvent @event)
{
throw new ArgumentException("null identifier");
}
- EntityKey key = new EntityKey(id, persister, source.EntityMode);
+ EntityKey key = source.GenerateEntityKey(id, persister);
persistenceContext.RemoveProxy(key);
if (!li.IsUninitialized)
{
View
2  src/NHibernate/Event/Default/DefaultFlushEntityEventListener.cs
@@ -522,7 +522,7 @@ private object[] GetDatabaseSnapshot(ISessionImplementor session, IEntityPersist
else
{
//TODO: optimize away this lookup for entities w/o unsaved-value="undefined"
- EntityKey entityKey = new EntityKey(id, persister, session.EntityMode);
+ EntityKey entityKey = session.GenerateEntityKey(id, persister);
return session.PersistenceContext.GetCachedDatabaseSnapshot(entityKey);
}
}
View
2  src/NHibernate/Event/Default/DefaultInitializeCollectionEventListener.cs
@@ -80,7 +80,7 @@ private bool InitializeCollectionFromCache(object id, ICollectionPersister persi
{
ISessionFactoryImplementor factory = source.Factory;
- CacheKey ck = new CacheKey(id, persister.KeyType, persister.Role, source.EntityMode, factory);
+ CacheKey ck = source.GenerateCacheKey(id, persister.KeyType, persister.Role);
object ce = persister.Cache.Get(ck, source.Timestamp);
if (factory.Statistics.IsStatisticsEnabled)
View
8 src/NHibernate/Event/Default/DefaultLoadEventListener.cs
@@ -65,7 +65,7 @@ public virtual void OnLoad(LoadEvent @event, LoadType loadType)
}
}
- EntityKey keyToLoad = new EntityKey(@event.EntityId, persister, source.EntityMode);
+ EntityKey keyToLoad = source.GenerateEntityKey(@event.EntityId, persister);
try
{
if (loadType.IsNakedEntityReturned)
@@ -248,7 +248,7 @@ protected virtual object LockAndLoad(LoadEvent @event, IEntityPersister persiste
CacheKey ck;
if (persister.HasCache)
{
- ck = new CacheKey(@event.EntityId, persister.IdentifierType, persister.RootEntityName, source.EntityMode, source.Factory);
+ ck = source.GenerateCacheKey(@event.EntityId, persister.IdentifierType, persister.RootEntityName);
sLock = persister.Cache.Lock(ck, null);
}
else
@@ -420,7 +420,7 @@ protected virtual object LoadFromSecondLevelCache(LoadEvent @event, IEntityPersi
{
ISessionFactoryImplementor factory = source.Factory;
- CacheKey ck = new CacheKey(@event.EntityId, persister.IdentifierType, persister.RootEntityName, source.EntityMode, factory);
+ CacheKey ck = source.GenerateCacheKey(@event.EntityId, persister.IdentifierType, persister.RootEntityName);
object ce = persister.Cache.Get(ck, source.Timestamp);
if (factory.Statistics.IsStatisticsEnabled)
@@ -468,7 +468,7 @@ private object AssembleCacheEntry(CacheEntry entry, object id, IEntityPersister
object result = optionalObject ?? session.Instantiate(subclassPersister, id);
// make it circular-reference safe
- EntityKey entityKey = new EntityKey(id, subclassPersister, session.EntityMode);
+ EntityKey entityKey = session.GenerateEntityKey(id, subclassPersister);
TwoPhaseLoad.AddUninitializedCachedEntity(entityKey, result, subclassPersister, LockMode.None, entry.AreLazyPropertiesUnfetched, entry.Version, session);
IType[] types = subclassPersister.PropertyTypes;
View
4 src/NHibernate/Event/Default/DefaultMergeEventListener.cs
@@ -138,7 +138,7 @@ public virtual void OnMerge(MergeEvent @event, IDictionary copiedAlready)
object id = persister.GetIdentifier(entity, source.EntityMode);
if (id != null)
{
- EntityKey key = new EntityKey(id, persister, source.EntityMode);
+ EntityKey key = source.GenerateEntityKey(id, persister);
object managedEntity = source.PersistenceContext.GetEntity(key);
entry = source.PersistenceContext.GetEntry(managedEntity);
if (entry != null)
@@ -437,7 +437,7 @@ private static bool ExistsInDatabase(object entity, IEventSource source, IEntity
object id = persister.GetIdentifier(entity, source.EntityMode);
if (id != null)
{
- EntityKey key = new EntityKey(id, persister, source.EntityMode);
+ EntityKey key = source.GenerateEntityKey(id, persister);
object managedEntity = source.PersistenceContext.GetEntity(key);
entry = source.PersistenceContext.GetEntry(managedEntity);
}
View
6 src/NHibernate/Event/Default/DefaultRefreshEventListener.cs
@@ -56,7 +56,7 @@ public virtual void OnRefresh(RefreshEvent @event, IDictionary refreshedAlready)
{
log.Debug("refreshing transient " + MessageHelper.InfoString(persister, id, source.Factory));
}
- EntityKey key = new EntityKey(id, persister, source.EntityMode);
+ EntityKey key = source.GenerateEntityKey(id, persister);
if (source.PersistenceContext.GetEntry(key) != null)
{
throw new PersistentObjectException("attempted to refresh transient instance when persistent instance was already associated with the Session: " +
@@ -84,7 +84,7 @@ public virtual void OnRefresh(RefreshEvent @event, IDictionary refreshedAlready)
if (e != null)
{
- EntityKey key = new EntityKey(id, persister, source.EntityMode);
+ EntityKey key = source.GenerateEntityKey(id, persister);
source.PersistenceContext.RemoveEntity(key);
if (persister.HasCollections)
new EvictVisitor(source).Process(obj, persister);
@@ -92,7 +92,7 @@ public virtual void OnRefresh(RefreshEvent @event, IDictionary refreshedAlready)
if (persister.HasCache)
{
- CacheKey ck = new CacheKey(id, persister.IdentifierType, persister.RootEntityName, source.EntityMode, source.Factory);
+ CacheKey ck = source.GenerateCacheKey(id, persister.IdentifierType, persister.RootEntityName);
persister.Cache.Remove(ck);
}
View
6 src/NHibernate/Event/Default/DefaultReplicateEventListener.cs
@@ -95,7 +95,7 @@ public virtual void OnReplicate(ReplicateEvent @event)
}
bool regenerate = persister.IsIdentifierAssignedByInsert; // prefer re-generation of identity!
- EntityKey key = regenerate ? null : new EntityKey(id, persister, source.EntityMode);
+ EntityKey key = regenerate ? null : source.GenerateEntityKey(id, persister);
PerformSaveOrReplicate(entity, key, persister, regenerate, replicationMode, source, true);
}
@@ -113,8 +113,8 @@ private void PerformReplication(object entity, object id, object version, IEntit
source.PersistenceContext.AddEntity(
entity,
persister.IsMutable ? Status.Loaded : Status.ReadOnly,
- null,
- new EntityKey(id, persister, source.EntityMode),
+ null,
+ source.GenerateEntityKey(id, persister),
version,
LockMode.None,
true,
View
2  src/NHibernate/Event/Default/DefaultSaveOrUpdateEventListener.cs
@@ -229,7 +229,7 @@ protected virtual void PerformUpdate(SaveOrUpdateEvent @event, object entity, IE
IEventSource source = @event.Session;
- EntityKey key = new EntityKey(@event.RequestedId, persister, source.EntityMode);
+ EntityKey key = source.GenerateEntityKey(@event.RequestedId, persister);
source.PersistenceContext.CheckUniqueness(key, entity);
View
16 src/NHibernate/Impl/AbstractSessionImpl.cs
@@ -3,6 +3,7 @@
using System.Collections.Generic;
using System.Data;
using NHibernate.AdoNet;
+using NHibernate.Cache;
using NHibernate.Collection;
using NHibernate.Engine;
using NHibernate.Engine.Query;
@@ -71,6 +72,21 @@ public void Initialize()
public abstract object ImmediateLoad(string entityName, object id);
public abstract long Timestamp { get; }
+ public EntityKey GenerateEntityKey(object id, IEntityPersister persister)
+ {
+ return GenerateEntityKey(id, persister, EntityMode);
+ }
+
+ protected EntityKey GenerateEntityKey(object id, IEntityPersister persister, EntityMode entityMode)
+ {
+ return new EntityKey(id, persister, entityMode);
+ }
+
+ public CacheKey GenerateCacheKey(object id, IType type, string entityOrRoleName)
+ {
+ return new CacheKey(id, type, entityOrRoleName, EntityMode, Factory);
+ }
+
public ISessionFactoryImplementor Factory
{
get { return factory; }
View
20 src/NHibernate/Impl/SessionFactoryImpl.cs
@@ -847,7 +847,7 @@ public void Evict(System.Type persistentClass, object id)
{
log.Debug("evicting second-level cache: " + MessageHelper.InfoString(p, id));
}
- CacheKey ck = new CacheKey(id, p.IdentifierType, p.RootEntityName, EntityMode.Poco, this);
+ CacheKey ck = GenerateCacheKeyForEvict(id, p.IdentifierType, p.RootEntityName);
p.Cache.Remove(ck);
}
}
@@ -887,7 +887,7 @@ public void EvictEntity(string entityName, object id)
{
log.Debug("evicting second-level cache: " + MessageHelper.InfoString(p, id, this));
}
- CacheKey cacheKey = new CacheKey(id, p.IdentifierType, p.RootEntityName, EntityMode.Poco, this);
+ CacheKey cacheKey = GenerateCacheKeyForEvict(id, p.IdentifierType, p.RootEntityName);
p.Cache.Remove(cacheKey);
}
}
@@ -901,11 +901,25 @@ public void EvictCollection(string roleName, object id)
{
log.Debug("evicting second-level cache: " + MessageHelper.InfoString(p, id));
}
- CacheKey ck = new CacheKey(id, p.KeyType, p.Role, EntityMode.Poco, this);
+ CacheKey ck = GenerateCacheKeyForEvict(id, p.KeyType, p.Role);
p.Cache.Remove(ck);
}
}
+ private CacheKey GenerateCacheKeyForEvict(object id, IType type, string entityOrRoleName)
+ {
+ // if there is a session context, use that to generate the key.
+ if (CurrentSessionContext != null)
+ {
+ return CurrentSessionContext
+ .CurrentSession()
+ .GetSessionImplementation()
+ .GenerateCacheKey(id, type, entityOrRoleName);
+ }
+
+ return new CacheKey(id, type, entityOrRoleName, EntityMode.Poco, this);
+ }
+
public void EvictCollection(string roleName)
{
ICollectionPersister p = GetCollectionPersister(roleName);
View
4 src/NHibernate/Impl/StatelessSessionImpl.cs
@@ -70,7 +70,7 @@ public override object InternalLoad(string entityName, object id, bool eager, bo
{
CheckAndUpdateSessionStatus();
IEntityPersister persister = Factory.GetEntityPersister(entityName);
- object loaded = temporaryPersistenceContext.GetEntity(new EntityKey(id, persister, EntityMode.Poco));
+ object loaded = temporaryPersistenceContext.GetEntity(GenerateEntityKey(id, persister, EntityMode.Poco));
if (loaded != null)
{
return loaded;
@@ -827,7 +827,7 @@ public void Refresh(string entityName, object entity, LockMode lockMode)
if (persister.HasCache)
{
- CacheKey ck = new CacheKey(id, persister.IdentifierType, persister.RootEntityName, EntityMode, Factory);
+ CacheKey ck = GenerateCacheKey(id, persister.IdentifierType, persister.RootEntityName);
persister.Cache.Remove(ck);
}
View
3  src/NHibernate/Loader/Collection/BatchingCollectionInitializer.cs
@@ -27,8 +27,7 @@ public BatchingCollectionInitializer(ICollectionPersister collectionPersister, i
public void Initialize(object id, ISessionImplementor session)
{
object[] batch =
- session.PersistenceContext.BatchFetchQueue.GetCollectionBatch(collectionPersister, id, batchSizes[0],
- session.EntityMode);
+ session.PersistenceContext.BatchFetchQueue.GetCollectionBatch(collectionPersister, id, batchSizes[0]);
for (int i = 0; i < batchSizes.Length; i++)
{
View
2  src/NHibernate/Loader/Entity/BatchingEntityLoader.cs
@@ -47,7 +47,7 @@ private object GetObjectFromList(IList results, object id, ISessionImplementor s
public object Load(object id, object optionalObject, ISessionImplementor session)
{
object[] batch =
- session.PersistenceContext.BatchFetchQueue.GetEntityBatch(persister, id, batchSizes[0], session.EntityMode);
+ session.PersistenceContext.BatchFetchQueue.GetEntityBatch(persister, id, batchSizes[0]);
for (int i = 0; i < batchSizes.Length - 1; i++)
{
View
4 src/NHibernate/Loader/Loader.cs
@@ -313,7 +313,7 @@ internal static EntityKey GetOptionalObjectKey(QueryParameters queryParameters,
if (optionalObject != null && !string.IsNullOrEmpty(optionalEntityName))
{
- return new EntityKey(optionalId, session.GetEntityPersister(optionalEntityName, optionalObject), session.EntityMode);
+ return session.GenerateEntityKey(optionalId, session.GetEntityPersister(optionalEntityName, optionalObject));
}
else
{
@@ -799,7 +799,7 @@ private EntityKey GetKeyFromResultSet(int i, IEntityPersister persister, object
}
}
- return resultId == null ? null : new EntityKey(resultId, persister, session.EntityMode);
+ return resultId == null ? null : session.GenerateEntityKey(resultId, persister);
}
/// <summary>
View
2  src/NHibernate/Persister/Collection/AbstractCollectionPersister.cs
@@ -611,7 +611,7 @@ private ICollectionInitializer GetSubselectInitializer(object key, ISessionImple
IPersistenceContext persistenceContext = session.PersistenceContext;
SubselectFetch subselect =
- persistenceContext.BatchFetchQueue.GetSubselect(new EntityKey(key, OwnerEntityPersister, session.EntityMode));
+ persistenceContext.BatchFetchQueue.GetSubselect(session.GenerateEntityKey(key, OwnerEntityPersister));
if (subselect == null)
{
View
6 src/NHibernate/Persister/Entity/AbstractEntityPersister.cs
@@ -1208,7 +1208,7 @@ public virtual object InitializeLazyProperty(string fieldName, object entity, IS
if (HasCache)
{
- CacheKey cacheKey = new CacheKey(id, IdentifierType, EntityName, session.EntityMode, Factory);
+ CacheKey cacheKey = session.GenerateCacheKey(id, IdentifierType, EntityName);
object ce = Cache.Get(cacheKey, session.Timestamp);
if (ce != null)
{
@@ -3076,7 +3076,7 @@ public void Delete(object id, object version, object obj, ISessionImplementor se
// first we need to locate the "loaded" state
//
// Note, it potentially could be a proxy, so perform the location the safe way...
- EntityKey key = new EntityKey(id, this, session.EntityMode);
+ EntityKey key = session.GenerateEntityKey(id, this);
object entity = session.PersistenceContext.GetEntity(key);
if (entity != null)
{
@@ -3719,7 +3719,7 @@ public virtual void AfterReassociate(object entity, ISessionImplementor session)
// check to see if it is in the second-level cache
if (HasCache)
{
- CacheKey ck = new CacheKey(id, IdentifierType, RootEntityName, session.EntityMode, session.Factory);
+ CacheKey ck = session.GenerateCacheKey(id, IdentifierType, RootEntityName);
if (Cache.Get(ck, session.Timestamp) != null)
return false;
}
View
2  src/NHibernate/Persister/Entity/NamedQueryLoader.cs
@@ -46,7 +46,7 @@ public object Load(object id, object optionalObject, ISessionImplementor session
// now look up the object we are really interested in!
// (this lets us correctly handle proxies and multi-row
// or multi-column queries)
- return session.PersistenceContext.GetEntity(new EntityKey(id, persister, session.EntityMode));
+ return session.PersistenceContext.GetEntity(session.GenerateEntityKey(id, persister));
}
}
}
View
4 src/NHibernate/Proxy/AbstractLazyInitializer.cs
@@ -199,7 +199,7 @@ public object GetImplementation()
/// <returns>The Persistent Object this proxy is Proxying, or <see langword="null" />.</returns>
public object GetImplementation(ISessionImplementor s)
{
- EntityKey key = new EntityKey(Identifier, s.Factory.GetEntityPersister(EntityName), s.EntityMode);
+ EntityKey key = s.GenerateEntityKey(Identifier, s.Factory.GetEntityPersister(EntityName));
return s.PersistenceContext.GetEntity(key);
}
@@ -249,7 +249,7 @@ private static EntityKey GenerateEntityKeyOrNull(object id, ISessionImplementor
if (id == null || s == null || entityName == null)
return null;
- return new EntityKey(id, s.Factory.GetEntityPersister(entityName), s.EntityMode);
+ return s.GenerateEntityKey(id, s.Factory.GetEntityPersister(entityName));
}
private void CheckTargetState()
View
2  src/NHibernate/Proxy/Poco/BasicLazyInitializer.cs
@@ -111,7 +111,7 @@ public virtual object Invoke(MethodInfo method, object[] args, object proxy)
if (Target == null & Session != null)
{
- EntityKey key = new EntityKey(Identifier, Session.Factory.GetEntityPersister(EntityName), Session.EntityMode);
+ EntityKey key = Session.GenerateEntityKey(Identifier, Session.Factory.GetEntityPersister(EntityName));
object entity = Session.PersistenceContext.GetEntity(key);
if (entity != null)
SetImplementation(entity);
View
2  src/NHibernate/Type/ManyToOneType.cs
@@ -92,7 +92,7 @@ private void ScheduleBatchLoadIfNeeded(object id, ISessionImplementor session)
if (uniqueKeyPropertyName == null && id != null)
{
IEntityPersister persister = session.Factory.GetEntityPersister(GetAssociatedEntityName());
- EntityKey entityKey = new EntityKey(id, persister, session.EntityMode);
+ EntityKey entityKey = session.GenerateEntityKey(id, persister);
if (!session.PersistenceContext.ContainsEntity(entityKey))
{
session.PersistenceContext.BatchFetchQueue.AddBatchLoadableEntityKey(entityKey);
View
4 src/NHibernate/Type/OneToOneType.cs
@@ -74,7 +74,7 @@ public override bool IsNull(object owner, ISessionImplementor session)
IEntityPersister ownerPersister = session.Factory.GetEntityPersister(entityName);
object id = session.GetContextEntityIdentifier(owner);
- EntityKey entityKey = new EntityKey(id, ownerPersister, session.EntityMode);
+ EntityKey entityKey = session.GenerateEntityKey(id, ownerPersister);
return session.PersistenceContext.IsPropertyNull(entityKey, PropertyName);
}
@@ -104,7 +104,7 @@ public override object Hydrate(IDataReader rs, string[] names, ISessionImplement
object[] values = ownerIdType.GetPropertyValues(identifier, session);
object id = componentType.ResolveIdentifier(values, session, null);
IEntityPersister persister = session.Factory.GetEntityPersister(type.ReturnedClass.FullName);
- var key = new EntityKey(id, persister, session.EntityMode);
+ var key = session.GenerateEntityKey(id, persister);
return session.PersistenceContext.GetEntity(key);
}
}
Please sign in to comment.
Something went wrong with that request. Please try again.