From 495b795fe8a02241089c10d3287bb9fb342b3eeb Mon Sep 17 00:00:00 2001 From: Roman Artiukhin Date: Fri, 21 Jul 2023 20:52:40 +0300 Subject: [PATCH] Refactoring --- .../Entity/AbstractEntityPersister.cs | 24 ++++++++++++++----- .../Entity/SingleTableEntityPersister.cs | 6 ++--- 2 files changed, 21 insertions(+), 9 deletions(-) diff --git a/src/NHibernate/Persister/Entity/AbstractEntityPersister.cs b/src/NHibernate/Persister/Entity/AbstractEntityPersister.cs index fb603da740a..5e2f3e4748d 100644 --- a/src/NHibernate/Persister/Entity/AbstractEntityPersister.cs +++ b/src/NHibernate/Persister/Entity/AbstractEntityPersister.cs @@ -263,7 +263,7 @@ public virtual void BindValues(DbCommand ps) // This must be a Lazy, because instances of this class must be thread safe. private readonly Lazy defaultUniqueKeyPropertyNamesForSelectId; - private readonly Dictionary propertyTableNumbersByNameAndSubclass = new Dictionary(); + private readonly Dictionary propertySubclassJoinTableNumbersByName; protected AbstractEntityPersister(PersistentClass persistentClass, ICacheConcurrencyStrategy cache, ISessionFactoryImplementor factory) @@ -441,6 +441,17 @@ protected AbstractEntityPersister(PersistentClass persistentClass, ICacheConcurr List columnSelectables = new List(); List propNullables = new List(); + if (persistentClass.SubclassJoinClosureIterator.Any()) + { + propertySubclassJoinTableNumbersByName = new Dictionary(); + foreach (Property prop in persistentClass.SubclassPropertyClosureIterator) + { + var joinNumber = persistentClass.GetJoinNumber(prop); + if (joinNumber != 0) + propertySubclassJoinTableNumbersByName[prop.PersistentClass.EntityName + '.' + prop.Name] = joinNumber; + } + } + foreach (Property prop in persistentClass.SubclassPropertyClosureIterator) { names.Add(prop.Name); @@ -449,8 +460,6 @@ protected AbstractEntityPersister(PersistentClass persistentClass, ICacheConcurr definedBySubclass.Add(isDefinedBySubclass); propNullables.Add(prop.IsOptional || isDefinedBySubclass); //TODO: is this completely correct? types.Add(prop.Type); - propertyTableNumbersByNameAndSubclass[prop.PersistentClass.EntityName + '.' + prop.Name] = - persistentClass.GetJoinNumber(prop); string[] cols = new string[prop.ColumnSpan]; string[] forms = new string[prop.ColumnSpan]; @@ -1125,12 +1134,15 @@ protected virtual bool IsIdOfTable(int property, int table) protected abstract int GetSubclassPropertyTableNumber(int i); - internal int GetSubclassPropertyTableNumber(string propertyName, string entityName) + internal int GetSubclassJoinPropertyTableNumber(string propertyName, string entityName) { + if (propertySubclassJoinTableNumbersByName == null) + return 0; + var type = propertyMapping.ToType(propertyName); if (type.IsAssociationType && ((IAssociationType) type).UseLHSPrimaryKey) return 0; - propertyTableNumbersByNameAndSubclass.TryGetValue(entityName + '.' + propertyName, out var tabnum); + propertySubclassJoinTableNumbersByName.TryGetValue(entityName + '.' + propertyName, out var tabnum); return tabnum; } @@ -4878,7 +4890,7 @@ internal SqlString GenerateSequentialSelect(AbstractEntityPersister subclassPers var classes = subclassPersister.PropertySubclassNames; for (var i = 0; i < props.Length; i++) { - var propTableNumber = GetSubclassPropertyTableNumber(props[i], classes[i]); + var propTableNumber = GetSubclassJoinPropertyTableNumber(props[i], classes[i]); if (IsSubclassTableSequentialSelect(propTableNumber) && !IsSubclassTableLazy(propTableNumber)) { tableNumbers.Add(propTableNumber); diff --git a/src/NHibernate/Persister/Entity/SingleTableEntityPersister.cs b/src/NHibernate/Persister/Entity/SingleTableEntityPersister.cs index 9aa8a71a3e4..3b1f0c5f7a8 100644 --- a/src/NHibernate/Persister/Entity/SingleTableEntityPersister.cs +++ b/src/NHibernate/Persister/Entity/SingleTableEntityPersister.cs @@ -696,7 +696,7 @@ protected override void AddDiscriminatorToInsert(SqlInsertBuilder insert) protected override bool IsSubclassPropertyDeferred(string propertyName, string entityName) { return - hasSequentialSelects && IsSubclassTableSequentialSelect(base.GetSubclassPropertyTableNumber(propertyName, entityName)); + hasSequentialSelects && IsSubclassTableSequentialSelect(GetSubclassPropertyTableNumber(propertyName, entityName)); } protected override bool IsPropertyDeferred(int propertyIndex) @@ -713,9 +713,9 @@ public override bool HasSequentialSelect //Since v5.3 [Obsolete("This method has no more usage in NHibernate and will be removed in a future version.")] - public new int GetSubclassPropertyTableNumber(string propertyName, string entityName) + public int GetSubclassPropertyTableNumber(string propertyName, string entityName) { - return base.GetSubclassPropertyTableNumber(propertyName, entityName); + return GetSubclassJoinPropertyTableNumber(propertyName, entityName); } //Since v5.3