private void InitLaziness(ICollectionPropertiesMapping collectionMapping, Mapping.Collection fetchable) { var lazyMapping = collectionMapping.Lazy; if (!lazyMapping.HasValue) { fetchable.IsLazy = mappings.DefaultLazy; fetchable.ExtraLazy = false; return; } switch (lazyMapping.Value) { case HbmCollectionLazy.True: fetchable.IsLazy = true; break; case HbmCollectionLazy.False: fetchable.IsLazy = false; fetchable.ExtraLazy = false; break; case HbmCollectionLazy.Extra: fetchable.IsLazy = true; fetchable.ExtraLazy = true; break; default: throw new ArgumentOutOfRangeException(); } }
public Mapping.Collection Create(ICollectionPropertiesMapping collectionMapping, string className, string propertyFullPath, PersistentClass owner, System.Type containingType, IDictionary<string, MetaAttribute> inheritedMetas) { var collectionType = collectionMapping.GetType(); if (collectionType == typeof (HbmBag)) { return CreateBag((HbmBag)collectionMapping, className, propertyFullPath, owner, containingType, inheritedMetas); } else if (collectionType == typeof (HbmSet)) { return CreateSet((HbmSet)collectionMapping, className, propertyFullPath, owner, containingType, inheritedMetas); } else if (collectionType == typeof (HbmList)) { return CreateList((HbmList)collectionMapping, className, propertyFullPath, owner, containingType, inheritedMetas); } else if (collectionType == typeof (HbmMap)) { return CreateMap((HbmMap)collectionMapping, className, propertyFullPath, owner, containingType, inheritedMetas); } else if (collectionType == typeof (HbmIdbag)) { return CreateIdentifierBag((HbmIdbag)collectionMapping, className, propertyFullPath, owner, containingType, inheritedMetas); } else if (collectionType == typeof (HbmArray)) { return CreateArray((HbmArray)collectionMapping, className, propertyFullPath, owner, containingType, inheritedMetas); } else if (collectionType == typeof (HbmPrimitiveArray)) { return CreatePrimitiveArray((HbmPrimitiveArray)collectionMapping, className, propertyFullPath, owner, containingType, inheritedMetas); } throw new MappingException("Not supported collection mapping element:" + collectionType); }
public Mapping.Collection Create(ICollectionPropertiesMapping collectionMapping, string className, string propertyFullPath, PersistentClass owner, System.Type containingType, IDictionary <string, MetaAttribute> inheritedMetas) { var collectionType = collectionMapping.GetType(); if (collectionType == typeof(HbmBag)) { return(CreateBag((HbmBag)collectionMapping, className, propertyFullPath, owner, containingType, inheritedMetas)); } else if (collectionType == typeof(HbmSet)) { return(CreateSet((HbmSet)collectionMapping, className, propertyFullPath, owner, containingType, inheritedMetas)); } else if (collectionType == typeof(HbmList)) { return(CreateList((HbmList)collectionMapping, className, propertyFullPath, owner, containingType, inheritedMetas)); } else if (collectionType == typeof(HbmMap)) { return(CreateMap((HbmMap)collectionMapping, className, propertyFullPath, owner, containingType, inheritedMetas)); } else if (collectionType == typeof(HbmIdbag)) { return(CreateIdentifierBag((HbmIdbag)collectionMapping, className, propertyFullPath, owner, containingType, inheritedMetas)); } else if (collectionType == typeof(HbmArray)) { return(CreateArray((HbmArray)collectionMapping, className, propertyFullPath, owner, containingType, inheritedMetas)); } else if (collectionType == typeof(HbmPrimitiveArray)) { return(CreatePrimitiveArray((HbmPrimitiveArray)collectionMapping, className, propertyFullPath, owner, containingType, inheritedMetas)); } throw new MappingException("Not supported collection mapping element:" + collectionType); }
private void AddCollectionSecondPass(ICollectionPropertiesMapping collectionMapping, Mapping.Collection model, IDictionary <string, MetaAttribute> inheritedMetas) { mappings.AddSecondPass(delegate(IDictionary <string, PersistentClass> persistentClasses) { PreCollectionSecondPass(model); BindCollectionSecondPass(collectionMapping, model, persistentClasses, inheritedMetas); PostCollectionSecondPass(model); }); }
private void InitOuterJoinFetchSetting(ICollectionPropertiesMapping collectionMapping, Mapping.Collection model) { FetchMode fetchStyle = FetchMode.Default; if (!collectionMapping.FetchMode.HasValue) { if (collectionMapping.OuterJoin.HasValue) { // use old (HB 2.1) defaults if outer-join is specified switch (collectionMapping.OuterJoin.Value) { case HbmOuterJoinStrategy.Auto: fetchStyle = FetchMode.Default; break; case HbmOuterJoinStrategy.True: fetchStyle = FetchMode.Join; break; case HbmOuterJoinStrategy.False: fetchStyle = FetchMode.Select; break; default: throw new ArgumentOutOfRangeException(); } } } else { switch (collectionMapping.FetchMode.Value) { case HbmCollectionFetchMode.Select: fetchStyle = FetchMode.Select; break; case HbmCollectionFetchMode.Join: fetchStyle = FetchMode.Join; break; case HbmCollectionFetchMode.Subselect: fetchStyle = FetchMode.Select; model.IsSubselectLoadable = true; model.Owner.HasSubselectLoadableCollections = true; break; default: throw new ArgumentOutOfRangeException(); } } model.FetchMode = fetchStyle; }
/// <remarks> /// Called for all collections /// </remarks> private void BindCollectionSecondPass(ICollectionPropertiesMapping collectionMapping, Mapping.Collection model, IDictionary <string, PersistentClass> persistentClasses, IDictionary <string, MetaAttribute> inheritedMetas) { if (model.IsOneToMany) { var oneToMany = (OneToMany)model.Element; string associatedEntityName = oneToMany.ReferencedEntityName; PersistentClass persistentClass; if (persistentClasses.TryGetValue(associatedEntityName, out persistentClass) == false) { throw new MappingException("Association references unmapped class: " + associatedEntityName); } oneToMany.AssociatedClass = persistentClass; model.CollectionTable = persistentClass.Table; if (log.IsInfoEnabled) { log.Info("mapping collection: " + model.Role + " -> " + model.CollectionTable.Name); } } //CHECK if (!string.IsNullOrEmpty(collectionMapping.Check)) { model.CollectionTable.AddCheckConstraint(collectionMapping.Check); } BindKey(collectionMapping.Key, model); //contained elements: HbmCompositeElement compositeElementMapping; HbmElement elementMapping; HbmManyToAny manyToAnyMapping; HbmManyToMany manyToManyMapping; if ((elementMapping = collectionMapping.ElementRelationship as HbmElement) != null) { BindElement(elementMapping, model); } else if ((manyToManyMapping = collectionMapping.ElementRelationship as HbmManyToMany) != null) { BindManyToMany(manyToManyMapping, model); } else if ((compositeElementMapping = collectionMapping.ElementRelationship as HbmCompositeElement) != null) { BindCompositeElement(compositeElementMapping, model, inheritedMetas); } else if ((manyToAnyMapping = collectionMapping.ElementRelationship as HbmManyToAny) != null) { BindManyToAny(manyToAnyMapping, model); } BindCache(collectionMapping.Cache, model); }
private void BindCollectionProperty(ICollectionPropertiesMapping collectionMapping, Property property) { property.Cascade = collectionMapping.Cascade ?? mappings.DefaultCascade; }
/// <remarks> /// Called for all collections /// </remarks> private void BindCollectionSecondPass(ICollectionPropertiesMapping collectionMapping, Mapping.Collection model, IDictionary<string, PersistentClass> persistentClasses, IDictionary<string, MetaAttribute> inheritedMetas) { if (model.IsOneToMany) { var oneToMany = (OneToMany)model.Element; string associatedEntityName = oneToMany.ReferencedEntityName; PersistentClass persistentClass; if (persistentClasses.TryGetValue(associatedEntityName, out persistentClass) == false) throw new MappingException("Association references unmapped class: " + associatedEntityName); oneToMany.AssociatedClass = persistentClass; model.CollectionTable = persistentClass.Table; if (model.IsInverse && persistentClass.JoinClosureSpan > 0) { // NH: bidirectional one-to-many with a class splitted in more tables; have to find in which table is the inverse side foreach (var joined in persistentClass.JoinClosureIterator) { if (collectionMapping.Key.Columns.Select(x=> x.name).All(x => joined.Table.ColumnIterator.Select(jc=> jc.Name).Contains(x))) { model.CollectionTable = joined.Table; break; } } } if (log.IsInfoEnabled) log.Info("mapping collection: " + model.Role + " -> " + model.CollectionTable.Name); } //CHECK if (!string.IsNullOrEmpty(collectionMapping.Check)) { model.CollectionTable.AddCheckConstraint(collectionMapping.Check); } BindKey(collectionMapping.Key, model); //contained elements: HbmCompositeElement compositeElementMapping; HbmElement elementMapping; HbmManyToAny manyToAnyMapping; HbmManyToMany manyToManyMapping; if ((elementMapping = collectionMapping.ElementRelationship as HbmElement) != null) { BindElement(elementMapping, model); } else if ((manyToManyMapping = collectionMapping.ElementRelationship as HbmManyToMany) != null) { BindManyToMany(manyToManyMapping, model); } else if ((compositeElementMapping = collectionMapping.ElementRelationship as HbmCompositeElement) != null) { BindCompositeElement(compositeElementMapping, model, inheritedMetas); } else if ((manyToAnyMapping = collectionMapping.ElementRelationship as HbmManyToAny) != null) { BindManyToAny(manyToAnyMapping, model); } BindCache(collectionMapping.Cache, model); if (NeedBackref(model)) { // for non-inverse one-to-many, with a not-null fk, add a backref! string entityName = ((OneToMany)model.Element).ReferencedEntityName; PersistentClass referenced = mappings.GetClass(entityName); var prop = new Backref(); prop.Name = '_' + model.OwnerEntityName + "." + collectionMapping.Name + "Backref"; prop.IsUpdateable = false; prop.IsSelectable = false; prop.CollectionRole = model.Role; prop.EntityName = model.Owner.EntityName; prop.Value = model.Key; referenced.AddProperty(prop); } }
private void AddCollectionSecondPass(ICollectionPropertiesMapping collectionMapping, Mapping.Collection model, IDictionary<string, MetaAttribute> inheritedMetas) { mappings.AddSecondPass(delegate(IDictionary<string, PersistentClass> persistentClasses) { PreCollectionSecondPass(model); BindCollectionSecondPass(collectionMapping, model, persistentClasses, inheritedMetas); PostCollectionSecondPass(model); }); }
private void InitLaziness(ICollectionPropertiesMapping collectionMapping, Mapping.Collection fetchable) { var lazyMapping = collectionMapping.Lazy; if(!lazyMapping.HasValue) { fetchable.IsLazy = mappings.DefaultLazy; fetchable.ExtraLazy = false; return; } switch (lazyMapping.Value) { case HbmCollectionLazy.True: fetchable.IsLazy = true; break; case HbmCollectionLazy.False: fetchable.IsLazy = false; fetchable.ExtraLazy = false; break; case HbmCollectionLazy.Extra: fetchable.IsLazy = true; fetchable.ExtraLazy = true; break; default: throw new ArgumentOutOfRangeException(); } }
/// <remarks> /// Called for all collections. <paramref name="containingType" /> parameter /// was added in NH to allow for reflection related to generic types. /// </remarks> private void BindCollection(ICollectionPropertiesMapping collectionMapping, Mapping.Collection model, string className, string path, System.Type containingType, IDictionary<string, MetaAttribute> inheritedMetas) { // ROLENAME model.Role = path; model.IsInverse = collectionMapping.Inverse; model.IsMutable = collectionMapping.Mutable; model.IsOptimisticLocked = collectionMapping.OptimisticLock; model.OrderBy = collectionMapping.OrderBy; model.Where = collectionMapping.Where; if (collectionMapping.BatchSize.HasValue) model.BatchSize = collectionMapping.BatchSize.Value; // PERSISTER if (!string.IsNullOrEmpty(collectionMapping.PersisterQualifiedName)) { model.CollectionPersisterClass = ClassForNameChecked(collectionMapping.PersisterQualifiedName, mappings, "could not instantiate collection persister class: {0}"); } if(!string.IsNullOrEmpty(collectionMapping.CollectionType)) { TypeDef typeDef = mappings.GetTypeDef(collectionMapping.CollectionType); if (typeDef != null) { model.TypeName = typeDef.TypeClass; model.TypeParameters = typeDef.Parameters; } else { model.TypeName = FullQualifiedClassName(collectionMapping.CollectionType, mappings); } } // FETCH STRATEGY InitOuterJoinFetchSetting(collectionMapping, model); // LAZINESS InitLaziness(collectionMapping, model); var oneToManyMapping = collectionMapping.ElementRelationship as HbmOneToMany; if (oneToManyMapping != null) { var oneToMany = new OneToMany(model.Owner); model.Element = oneToMany; BindOneToMany(oneToManyMapping, oneToMany); //we have to set up the table later!! yuck } else { //TABLE string tableName = !string.IsNullOrEmpty(collectionMapping.Table) ? mappings.NamingStrategy.TableName(collectionMapping.Table) : mappings.NamingStrategy.PropertyToTableName(className, path); string schema = string.IsNullOrEmpty(collectionMapping.Schema) ? mappings.SchemaName : collectionMapping.Schema; string catalog = string.IsNullOrEmpty(collectionMapping.Catalog) ? mappings.CatalogName : collectionMapping.Catalog; // TODO NH : add schema-action to the xsd model.CollectionTable = mappings.AddTable(schema, catalog, tableName, collectionMapping.Subselect, false, "all"); log.InfoFormat("Mapping collection: {0} -> {1}", model.Role, model.CollectionTable.Name); } //SORT var sortedAtt = collectionMapping.Sort; // unsorted, natural, comparator.class.name if (string.IsNullOrEmpty(sortedAtt) || sortedAtt.Equals("unsorted")) model.IsSorted = false; else { model.IsSorted = true; if (!sortedAtt.Equals("natural")) { string comparatorClassName = FullQualifiedClassName(sortedAtt, mappings); model.ComparerClassName = comparatorClassName; } } //ORPHAN DELETE (used for programmer error detection) var cascadeAtt = collectionMapping.Cascade; if (!string.IsNullOrEmpty(cascadeAtt) && cascadeAtt.IndexOf("delete-orphan") >= 0) model.HasOrphanDelete = true; // GENERIC bool? isGeneric = collectionMapping.Generic; System.Type collectionType = null; if (!isGeneric.HasValue && containingType != null) { collectionType = GetPropertyType(containingType, collectionMapping.Name, collectionMapping.Access); isGeneric = collectionType.IsGenericType; } model.IsGeneric = isGeneric.GetValueOrDefault(); if (model.IsGeneric) { // Determine the generic arguments using reflection if (collectionType == null) collectionType = GetPropertyType(containingType, collectionMapping.Name, collectionMapping.Access); System.Type[] genericArguments = collectionType.GetGenericArguments(); model.GenericArguments = genericArguments; } // CUSTOM SQL HandleCustomSQL(collectionMapping, model); if (collectionMapping.SqlLoader != null) model.LoaderName = collectionMapping.SqlLoader.queryref; new FiltersBinder(model, Mappings).Bind(collectionMapping.Filters); var key = collectionMapping.Key; if (key != null) model.ReferencedPropertyName = key.propertyref; }
/// <remarks> /// Called for all collections /// </remarks> private void BindCollectionSecondPass(ICollectionPropertiesMapping collectionMapping, Mapping.Collection model, IDictionary <string, PersistentClass> persistentClasses, IDictionary <string, MetaAttribute> inheritedMetas) { if (model.IsOneToMany) { var oneToMany = (OneToMany)model.Element; string associatedEntityName = oneToMany.ReferencedEntityName; PersistentClass persistentClass; if (persistentClasses.TryGetValue(associatedEntityName, out persistentClass) == false) { throw new MappingException("Association references unmapped class: " + associatedEntityName); } oneToMany.AssociatedClass = persistentClass; model.CollectionTable = persistentClass.Table; if (model.IsInverse && persistentClass.JoinClosureSpan > 0) { // NH: bidirectional one-to-many with a class splitted in more tables; have to find in which table is the inverse side foreach (var joined in persistentClass.JoinClosureIterator) { if (collectionMapping.Key.Columns.Select(x => x.name).All(x => joined.Table.ColumnIterator.Select(jc => jc.Name).Contains(x))) { model.CollectionTable = joined.Table; break; } } } if (log.IsInfoEnabled) { log.Info("mapping collection: " + model.Role + " -> " + model.CollectionTable.Name); } } //CHECK if (!string.IsNullOrEmpty(collectionMapping.Check)) { model.CollectionTable.AddCheckConstraint(collectionMapping.Check); } BindKey(collectionMapping.Key, model); //contained elements: HbmCompositeElement compositeElementMapping; HbmElement elementMapping; HbmManyToAny manyToAnyMapping; HbmManyToMany manyToManyMapping; if ((elementMapping = collectionMapping.ElementRelationship as HbmElement) != null) { BindElement(elementMapping, model); } else if ((manyToManyMapping = collectionMapping.ElementRelationship as HbmManyToMany) != null) { BindManyToMany(manyToManyMapping, model); } else if ((compositeElementMapping = collectionMapping.ElementRelationship as HbmCompositeElement) != null) { BindCompositeElement(compositeElementMapping, model, inheritedMetas); } else if ((manyToAnyMapping = collectionMapping.ElementRelationship as HbmManyToAny) != null) { BindManyToAny(manyToAnyMapping, model); } BindCache(collectionMapping.Cache, model); if (model.IsOneToMany && !model.IsInverse && !model.Key.IsNullable) { // for non-inverse one-to-many, with a not-null fk, add a backref! string entityName = ((OneToMany)model.Element).ReferencedEntityName; PersistentClass referenced = mappings.GetClass(entityName); Backref prop = new Backref(); prop.Name = '_' + model.OwnerEntityName + "." + collectionMapping.Name + "Backref"; prop.IsUpdateable = false; prop.IsSelectable = false; prop.CollectionRole = model.Role; prop.EntityName = model.Owner.EntityName; prop.Value = model.Key; referenced.AddProperty(prop); } }
/// <remarks> /// Called for all collections. <paramref name="containingType" /> parameter /// was added in NH to allow for reflection related to generic types. /// </remarks> private void BindCollection(ICollectionPropertiesMapping collectionMapping, Mapping.Collection model, string className, string path, System.Type containingType, IDictionary <string, MetaAttribute> inheritedMetas) { // ROLENAME model.Role = path; model.IsInverse = collectionMapping.Inverse; model.IsMutable = collectionMapping.Mutable; model.IsOptimisticLocked = collectionMapping.OptimisticLock; model.OrderBy = collectionMapping.OrderBy; model.Where = collectionMapping.Where; if (collectionMapping.BatchSize.HasValue) { model.BatchSize = collectionMapping.BatchSize.Value; } // PERSISTER if (!string.IsNullOrEmpty(collectionMapping.PersisterQualifiedName)) { model.CollectionPersisterClass = ClassForNameChecked(collectionMapping.PersisterQualifiedName, mappings, "could not instantiate collection persister class: {0}"); } if (!string.IsNullOrEmpty(collectionMapping.CollectionType)) { TypeDef typeDef = mappings.GetTypeDef(collectionMapping.CollectionType); if (typeDef != null) { model.TypeName = typeDef.TypeClass; model.TypeParameters = typeDef.Parameters; } else { model.TypeName = FullQualifiedClassName(collectionMapping.CollectionType, mappings); } } // FETCH STRATEGY InitOuterJoinFetchSetting(collectionMapping, model); // LAZINESS InitLaziness(collectionMapping, model); var oneToManyMapping = collectionMapping.ElementRelationship as HbmOneToMany; if (oneToManyMapping != null) { var oneToMany = new OneToMany(model.Owner); model.Element = oneToMany; BindOneToMany(oneToManyMapping, oneToMany); //we have to set up the table later!! yuck } else { //TABLE string tableName = !string.IsNullOrEmpty(collectionMapping.Table) ? mappings.NamingStrategy.TableName(collectionMapping.Table) : mappings.NamingStrategy.PropertyToTableName(className, path); string schema = string.IsNullOrEmpty(collectionMapping.Schema) ? mappings.SchemaName : collectionMapping.Schema; string catalog = string.IsNullOrEmpty(collectionMapping.Catalog) ? mappings.CatalogName : collectionMapping.Catalog; // TODO NH : add schema-action to the xsd model.CollectionTable = mappings.AddTable(schema, catalog, tableName, collectionMapping.Subselect, false, "all"); log.InfoFormat("Mapping collection: {0} -> {1}", model.Role, model.CollectionTable.Name); } //SORT var sortedAtt = collectionMapping.Sort; // unsorted, natural, comparator.class.name if (string.IsNullOrEmpty(sortedAtt) || sortedAtt.Equals("unsorted")) { model.IsSorted = false; } else { model.IsSorted = true; if (!sortedAtt.Equals("natural")) { string comparatorClassName = FullQualifiedClassName(sortedAtt, mappings); model.ComparerClassName = comparatorClassName; } } //ORPHAN DELETE (used for programmer error detection) var cascadeAtt = collectionMapping.Cascade; if (!string.IsNullOrEmpty(cascadeAtt) && cascadeAtt.IndexOf("delete-orphan") >= 0) { model.HasOrphanDelete = true; } // GENERIC bool?isGeneric = collectionMapping.Generic; System.Type collectionType = null; if (!isGeneric.HasValue && containingType != null) { collectionType = GetPropertyType(containingType, collectionMapping.Name, collectionMapping.Access); isGeneric = collectionType.IsGenericType; } model.IsGeneric = isGeneric ?? false; if (model.IsGeneric) { // Determine the generic arguments using reflection if (collectionType == null) { collectionType = GetPropertyType(containingType, collectionMapping.Name, collectionMapping.Access); } System.Type[] genericArguments = collectionType.GetGenericArguments(); model.GenericArguments = genericArguments; } // CUSTOM SQL HandleCustomSQL(collectionMapping, model); if (collectionMapping.SqlLoader != null) { model.LoaderName = collectionMapping.SqlLoader.queryref; } new FiltersBinder(model, Mappings).Bind(collectionMapping.Filters); var key = collectionMapping.Key; if (key != null) { model.ReferencedPropertyName = key.propertyref; } }
/// <remarks> /// Called for all collections /// </remarks> private void BindCollectionSecondPass(ICollectionPropertiesMapping collectionMapping, Mapping.Collection model, IDictionary<string, PersistentClass> persistentClasses, IDictionary<string, MetaAttribute> inheritedMetas) { if (model.IsOneToMany) { var oneToMany = (OneToMany)model.Element; string associatedEntityName = oneToMany.ReferencedEntityName; PersistentClass persistentClass; if (persistentClasses.TryGetValue(associatedEntityName, out persistentClass) == false) throw new MappingException("Association references unmapped class: " + associatedEntityName); oneToMany.AssociatedClass = persistentClass; model.CollectionTable = persistentClass.Table; if (log.IsInfoEnabled) log.Info("mapping collection: " + model.Role + " -> " + model.CollectionTable.Name); } //CHECK if (!string.IsNullOrEmpty(collectionMapping.Check)) { model.CollectionTable.AddCheckConstraint(collectionMapping.Check); } BindKey(collectionMapping.Key, model); //contained elements: HbmCompositeElement compositeElementMapping; HbmElement elementMapping; HbmManyToAny manyToAnyMapping; HbmManyToMany manyToManyMapping; if ((elementMapping = collectionMapping.ElementRelationship as HbmElement) != null) { BindElement(elementMapping, model); } else if ((manyToManyMapping = collectionMapping.ElementRelationship as HbmManyToMany) != null) { BindManyToMany(manyToManyMapping, model); } else if ((compositeElementMapping = collectionMapping.ElementRelationship as HbmCompositeElement) != null) { BindCompositeElement(compositeElementMapping, model, inheritedMetas); } else if ((manyToAnyMapping = collectionMapping.ElementRelationship as HbmManyToAny) != null) { BindManyToAny(manyToAnyMapping, model); } BindCache(collectionMapping.Cache, model); }