/// <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;
		}
		private void BindOneToMany(HbmOneToMany oneToManyMapping, OneToMany model)
		{
			model.ReferencedEntityName = GetEntityName(oneToManyMapping, mappings);

			model.IsIgnoreNotFound = oneToManyMapping.NotFoundMode == HbmNotFoundMode.Ignore;
		}
		public static void BindOneToMany( XmlNode node, OneToMany model, Mappings mappings )
		{
			model.Type = ( EntityType ) NHibernateUtil.Entity(
				ClassForNameChecked( node.Attributes[ "class" ].Value, mappings,
				                     "associated class not found: {0}" ) );
		}
		/// <remarks>
		/// Called for all collections
		/// </remarks>
		public static void BindCollection( XmlNode node, Mapping.Collection model, string className, string path, Mappings mappings )
		{
			//ROLENAME
			model.Role = StringHelper.Qualify( className, path );

			XmlAttribute inverseNode = node.Attributes[ "inverse" ];
			if( inverseNode != null )
			{
				model.IsInverse = StringHelper.BooleanValue( inverseNode.Value );
			}

			XmlAttribute orderNode = node.Attributes[ "order-by" ];
			if( orderNode != null )
			{
				model.OrderBy = orderNode.Value;
			}
			XmlAttribute whereNode = node.Attributes[ "where" ];
			if( whereNode != null )
			{
				model.Where = whereNode.Value;
			}
			XmlAttribute batchNode = node.Attributes[ "batch-size" ];
			if( batchNode != null )
			{
				model.BatchSize = Int32.Parse( batchNode.Value );
			}

			//PERSISTER
			XmlAttribute persisterNode = node.Attributes[ "persister" ];
			if( persisterNode == null )
			{
				//persister = CollectionPersisterImpl.class;
			}
			else
			{
				model.CollectionPersisterClass = ClassForNameChecked(
					persisterNode.Value, mappings,
					"could not instantiate collection persister class: {0}" );
			}

			InitOuterJoinFetchSetting( node, model );

			XmlNode oneToManyNode = node.SelectSingleNode( nsOneToMany, nsmgr );
			if( oneToManyNode != null )
			{
				OneToMany oneToMany = new OneToMany( model.Owner );
				model.Element = oneToMany;
				BindOneToMany( oneToManyNode, oneToMany, mappings );
				//we have to set up the table later!! yuck
			}
			else
			{
				//TABLE
				XmlAttribute tableNode = node.Attributes[ "table" ];
				string tableName;
				if( tableNode != null )
				{
					tableName = mappings.NamingStrategy.TableName( tableNode.Value );
				}
				else
				{
					tableName = mappings.NamingStrategy.PropertyToTableName( className, path );
				}
				XmlAttribute schemaNode = node.Attributes[ "schema" ];
				string schema = schemaNode == null ? mappings.SchemaName : schemaNode.Value;
				model.CollectionTable = mappings.AddTable( schema, tableName );

				log.Info( "Mapping collection: " + model.Role + " -> " + model.CollectionTable.Name );
			}

			//LAZINESS
			XmlAttribute lazyNode = node.Attributes[ "lazy" ];
			if( lazyNode != null )
			{
				model.IsLazy = StringHelper.BooleanValue( lazyNode.Value );
			}

			//SORT
			XmlAttribute sortedAtt = node.Attributes[ "sort" ];
			// unsorted, natural, comparator.class.name
			if( sortedAtt == null || sortedAtt.Value.Equals( "unsorted" ) )
			{
				model.IsSorted = false;
			}
			else
			{
				model.IsSorted = true;
				string comparatorClassName = FullClassName( sortedAtt.Value, mappings );
				if( !comparatorClassName.Equals( "natural" ) )
				{
					try
					{
						model.Comparer = ( IComparer ) Activator.CreateInstance( ReflectHelper.ClassForName( comparatorClassName ) );
					}
					catch
					{
						throw new MappingException( "could not instantiate comparer class: " + comparatorClassName );
					}
				}
			}

			//ORPHAN DELETE (used for programmer error detection)
			XmlAttribute cascadeAtt = node.Attributes[ "cascade" ];
			if( cascadeAtt != null && cascadeAtt.Value.Equals( "all-delete-orphan" ) )
			{
				model.OrphanDelete = true;
			}

			//set up second pass
			if( model is List )
			{
				mappings.AddSecondPass( new ListSecondPass( node, mappings, ( List ) model ) );
			}
			else if( model is Map )
			{
				mappings.AddSecondPass( new MapSecondPass( node, mappings, ( Map ) model ) );
			}
			else if( model is Set )
			{
				mappings.AddSecondPass( new SetSecondPass( node, mappings, ( Set ) model ) );
			}
			else if( model is IdentifierCollection )
			{
				mappings.AddSecondPass( new IdentifierCollectionSecondPass( node, mappings, ( IdentifierCollection ) model ) );
			}
			else
			{
				mappings.AddSecondPass( new CollectionSecondPass( node, mappings, model ) );
			}
		}
示例#5
0
		private void BindOneToMany(XmlNode node, OneToMany model)
		{
			model.ReferencedEntityName = ClassForNameChecked(node.Attributes["class"].Value, mappings,
				"associated class not found: {0}").FullName;

			string notFound = XmlHelper.GetAttributeValue(node, "not-found");
			model.IsIgnoreNotFound = "ignore".Equals(notFound);
		}
示例#6
0
		/// <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(XmlNode node, Mapping.Collection model, string className,
			string path, System.Type containingType)
		{
			// ROLENAME
			model.Role = StringHelper.Qualify(className, path);
			// TODO: H3.1 has just collection.setRole(path) here - why?

			XmlAttribute inverseNode = node.Attributes["inverse"];
			if (inverseNode != null)
				model.IsInverse = StringHelper.BooleanValue(inverseNode.Value);

			// TODO: H3.1 - not ported: mutable
			XmlAttribute olNode = node.Attributes["optimistic-lock"];
			model.IsOptimisticLocked = olNode == null || "true".Equals(olNode.Value);

			XmlAttribute orderNode = node.Attributes["order-by"];
			if (orderNode != null)
				model.OrderBy = orderNode.Value;
			XmlAttribute whereNode = node.Attributes["where"];
			if (whereNode != null)
				model.Where = whereNode.Value;
			XmlAttribute batchNode = node.Attributes["batch-size"];
			if (batchNode != null)
				model.BatchSize = int.Parse(batchNode.Value);

			// PERSISTER
			XmlAttribute persisterNode = node.Attributes["persister"];
			if (persisterNode == null)
			{
				//persister = CollectionPersisterImpl.class;
			}
			else
				model.CollectionPersisterClass = ClassForNameChecked(
					persisterNode.Value, mappings,
					"could not instantiate collection persister class: {0}");

			XmlAttribute typeNode = node.Attributes["collection-type"];
			if (typeNode != null)
				model.TypeName = typeNode.Value;

			// FETCH STRATEGY
			InitOuterJoinFetchSetting(node, model);

			if ("subselect".Equals(XmlHelper.GetAttributeValue(node, "fetch")))
			{
				model.IsSubselectLoadable = true;
				model.Owner.HasSubselectLoadableCollections = true;
			}

			// LAZINESS
			InitLaziness(node, model, "true", mappings.DefaultLazy);
			// TODO: H3.1 - lazy="extra"

			XmlNode oneToManyNode = node.SelectSingleNode(HbmConstants.nsOneToMany, namespaceManager);
			if (oneToManyNode != null)
			{
				OneToMany oneToMany = new OneToMany(model.Owner);
				model.Element = oneToMany;
				BindOneToMany(oneToManyNode, oneToMany);
				//we have to set up the table later!! yuck
			}
			else
			{
				//TABLE
				XmlAttribute tableNode = node.Attributes["table"];
				string tableName;
				if (tableNode != null)
					tableName = mappings.NamingStrategy.TableName(tableNode.Value);
				else
					tableName = mappings.NamingStrategy.PropertyToTableName(className, path);
				XmlAttribute schemaNode = node.Attributes["schema"];
				string schema = schemaNode == null ? mappings.SchemaName : schemaNode.Value;
				XmlAttribute catalogNode = node.Attributes["catalog"];
				string catalog = catalogNode == null ? mappings.CatalogName : catalogNode.Value;

				model.CollectionTable = mappings.AddTable(schema, catalog, tableName, null, false);

				log.InfoFormat("Mapping collection: {0} -> {1}", model.Role, model.CollectionTable.Name);
			}

			//SORT
			XmlAttribute sortedAtt = node.Attributes["sort"];
			// unsorted, natural, comparator.class.name
			if (sortedAtt == null || sortedAtt.Value.Equals("unsorted"))
				model.IsSorted = false;
			else
			{
				model.IsSorted = true;
				if (!sortedAtt.Value.Equals("natural"))
				{
					string comparatorClassName = FullClassName(sortedAtt.Value, mappings);
					try
					{
						model.Comparer = Activator.CreateInstance(ReflectHelper.ClassForName(comparatorClassName));
					}
					catch
					{
						throw new MappingException("could not instantiate comparer class: " + comparatorClassName);
					}
				}
			}

			//ORPHAN DELETE (used for programmer error detection)
			XmlAttribute cascadeAtt = node.Attributes["cascade"];
			if (cascadeAtt != null && cascadeAtt.Value.Equals("all-delete-orphan"))
				model.HasOrphanDelete = true;

			bool? isGeneric = null;

			XmlAttribute genericAtt = node.Attributes["generic"];
			if (genericAtt != null)
				isGeneric = bool.Parse(genericAtt.Value);

			System.Type collectionType = null;

			if (!isGeneric.HasValue && containingType != null)
			{
				collectionType = GetPropertyType(node, containingType, GetPropertyName(node));
				isGeneric = collectionType.IsGenericType;
			}

			model.IsGeneric = isGeneric ?? false;

			if (model.IsGeneric)
			{
				// Determine the generic arguments using reflection
				if (collectionType == null)
					collectionType = GetPropertyType(node, containingType, GetPropertyName(node));
				System.Type[] genericArguments = collectionType.GetGenericArguments();
				model.GenericArguments = genericArguments;
			}

			HandleCustomSQL(node, model);

			//set up second pass
			if (model is List)
				AddListSecondPass(node, (List)model);

			else if (model is Map)
				AddMapSecondPass(node, (Map)model);

			else if (model is Set)
				AddSetSecondPass(node, (Set)model);

			else if (model is IdentifierCollection)
				AddIdentifierCollectionSecondPass(node, (IdentifierCollection)model);

			else
				AddCollectionSecondPass(node, model);

			foreach (XmlNode filter in node.SelectNodes(HbmConstants.nsFilter, namespaceManager))
				ParseFilter(filter, model);

			XmlNode loader = node.SelectSingleNode(HbmConstants.nsLoader, namespaceManager);
			if (loader != null)
				model.LoaderName = XmlHelper.GetAttributeValue(loader, "query-ref");

			XmlNode key = node.SelectSingleNode(HbmConstants.nsKey, namespaceManager);
			if (key != null)
				model.ReferencedPropertyName = XmlHelper.GetAttributeValue(key, "property-ref");

		}
		private void BindOneToMany(XmlNode node, OneToMany model)
		{
			model.ReferencedEntityName = GetEntityName(node, mappings);

			string notFound = XmlHelper.GetAttributeValue(node, "not-found");
			model.IsIgnoreNotFound = "ignore".Equals(notFound);
		}