private static RootClass CreateMappingClasses()
        {
            var classMapping     = new RootClass();
            var componentMapping = new NHibernate.Mapping.Component(classMapping);

            var componentPropertyMapping = new Property(componentMapping);

            componentPropertyMapping.Name = "ComponentPropertyInClass";
            classMapping.AddProperty(componentPropertyMapping);

            var stringValue = new SimpleValue();

            stringValue.TypeName = typeof(string).FullName;

            var stringPropertyInComponentMapping = new Property(stringValue);

            stringPropertyInComponentMapping.Name = "StringPropertyInComponent";
            componentMapping.AddProperty(stringPropertyInComponentMapping);

            var componentType = (IAbstractComponentType)componentMapping.Type;

            Assume.That(CascadeStyle.None == stringPropertyInComponentMapping.CascadeStyle);
            Assume.That(CascadeStyle.None == componentType.GetCascadeStyle(0));
            Assume.That(CascadeStyle.None == componentPropertyMapping.CascadeStyle);

            return(classMapping);
        }
Exemplo n.º 2
0
		public PropertiesBinder(Mappings mappings, PersistentClass persistentClass, Dialect.Dialect dialect)
			: base(mappings, dialect)
		{
			this.persistentClass = persistentClass;
			entityName = persistentClass.EntityName;
			propertyBasePath = entityName;
			className = persistentClass.ClassName;
			mappedClass = persistentClass.MappedClass;
			componetDefaultNullable = true;
			component = null;
		}
Exemplo n.º 3
0
		public PropertiesBinder(Mappings mappings, Component component, string className, string path, bool isNullable, Dialect.Dialect dialect)
			: base(mappings, dialect)
		{
			persistentClass = component.Owner;
			this.component = component;
			entityName = className;
			this.className = component.ComponentClassName;
			mappedClass = component.ComponentClass;
			propertyBasePath = path;
			componetDefaultNullable = isNullable;
		}
		public void BindCompositeId(HbmCompositeId idSchema, PersistentClass rootClass)
		{
			if (idSchema == null)
				return;

			compositeId = new Component(rootClass);
			rootClass.Identifier = compositeId;

			if (idSchema.name == null)
			{
				BindComponent(null, "id", idSchema);
				rootClass.HasEmbeddedIdentifier = compositeId.IsEmbedded;
			}
			else
			{
				System.Type reflectedClass = GetPropertyType(rootClass.MappedClass,
					idSchema.name, idSchema);

				BindComponent(reflectedClass, idSchema.name, idSchema);

				Mapping.Property prop = new Mapping.Property(compositeId);
				BindProperty(prop, idSchema);
				rootClass.IdentifierProperty = prop;
			}

			compositeId.Table.SetIdentifierValue(compositeId);
			compositeId.NullValue = GetXmlEnumAttribute(idSchema.unsavedvalue);

			System.Type compIdClass = compositeId.ComponentClass;

            //<Simon date='26.04.2010'>
            if(compositeId.IsDynamic) return;
            //</Simon>
			if (!ReflectHelper.OverridesEquals(compIdClass))
				throw new MappingException(
					"composite-id class must override Equals(): " + compIdClass.FullName
					);

			if (!ReflectHelper.OverridesGetHashCode(compIdClass))
				throw new MappingException(
					"composite-id class must override GetHashCode(): " + compIdClass.FullName
					);
			// Serializability check not ported
		}
Exemplo n.º 5
0
		public void BindCompositeId(HbmCompositeId idSchema, PersistentClass rootClass)
		{
			if (idSchema == null)
			{
				return;
			}

			compositeId = new Component(rootClass);
			compositeId.IsKey = true;
			
			rootClass.Identifier = compositeId;

			if (idSchema.name == null)
			{
				BindComponent(null, "id", idSchema);
				rootClass.HasEmbeddedIdentifier = compositeId.IsEmbedded;
			}
			else
			{
				System.Type reflectedClass = GetPropertyType(rootClass.MappedClass, idSchema.name, idSchema);

				BindComponent(reflectedClass, idSchema.name, idSchema);

				var prop = new Property(compositeId);
				BindProperty(prop, idSchema);
				rootClass.IdentifierProperty = prop;
			}

			compositeId.Table.SetIdentifierValue(compositeId);
			compositeId.NullValue = idSchema.unsavedvalue.ToNullValue();

			System.Type compIdClass = compositeId.ComponentClass;
			if (!ReflectHelper.OverridesEquals(compIdClass))
			{
				throw new MappingException("composite-id class must override Equals(): " + compIdClass.FullName);
			}

			if (!ReflectHelper.OverridesGetHashCode(compIdClass))
			{
				throw new MappingException("composite-id class must override GetHashCode(): " + compIdClass.FullName);
			}
			// Serializability check not ported
		}
		public void BindCompositeId(HbmCompositeId idSchema, PersistentClass rootClass)
		{
			if (idSchema == null)
			{
				return;
			}

			compositeId = new Component(rootClass);
			compositeId.IsKey = true;

			rootClass.Identifier = compositeId;

			if (idSchema.name == null)
			{
				BindComponent(null, "id", idSchema);
				rootClass.HasEmbeddedIdentifier = compositeId.IsEmbedded;
			}
			else
			{
				System.Type reflectedClass = GetPropertyType(rootClass.MappedClass, idSchema.name, idSchema);

				BindComponent(reflectedClass, idSchema.name, idSchema);

				var prop = new Property(compositeId);
				BindProperty(prop, idSchema);
				rootClass.IdentifierProperty = prop;
			}

			compositeId.Table.SetIdentifierValue(compositeId);
			compositeId.NullValue = idSchema.unsavedvalue.ToNullValue();

			if (!compositeId.IsDynamic)
			{
				CheckEqualsAndGetHashCodeOverride();
			}
			// Serializability check not ported
		}
Exemplo n.º 7
0
		/// <remarks>
		/// Called for all collections
		/// </remarks>
		public static void BindCollectionSecondPass( XmlNode node, Mapping.Collection model, IDictionary persistentClasses, Mappings mappings )
		{
			if( model.IsOneToMany )
			{
				OneToMany oneToMany = ( OneToMany ) model.Element;
				System.Type assocClass = oneToMany.EntityType.AssociatedClass;
				PersistentClass persistentClass = ( PersistentClass ) persistentClasses[ assocClass ];
				if( persistentClass == null )
				{
					throw new MappingException( "Association references unmapped class: " + assocClass.Name );
				}
				oneToMany.AssociatedClass = persistentClass;
				model.CollectionTable = persistentClass.Table;

				if( log.IsInfoEnabled )
				{
					log.Info( "mapping collection: " + model.Role + " -> " + model.CollectionTable.Name );
				}
			}

			//CHECK
			XmlAttribute chNode = node.Attributes[ "check" ];
			if( chNode != null )
			{
				model.CollectionTable.AddCheckConstraint( chNode.Value );
			}

			//contained elements:
			foreach( XmlNode subnode in node.ChildNodes )
			{
				//I am only concerned with elements that are from the nhibernate namespace
				if( subnode.NamespaceURI != Configuration.MappingSchemaXMLNS )
				{
					continue;
				}

				string name = subnode.LocalName; //.Name;

				if( "key".Equals( name ) || "generated-key".Equals( name ) )
				{
					SimpleValue key = new SimpleValue( model.CollectionTable );
					BindSimpleValue( subnode, key, model.IsOneToMany, Mapping.Collection.DefaultKeyColumnName, mappings );
					key.Type = model.Owner.Identifier.Type;
					if( key.Type.ReturnedClass.IsArray )
					{
						throw new MappingException( "illegal use of an array as an identifier (arrays don't reimplement Equals)" );
					}
					model.Key = key;
				}
				else if( "element".Equals( name ) )
				{
					SimpleValue elt = new SimpleValue( model.CollectionTable );
					model.Element = elt;
					BindSimpleValue( subnode, elt, true, Mapping.Collection.DefaultElementColumnName, mappings );
				}
				else if( "many-to-many".Equals( name ) )
				{
					ManyToOne element = new ManyToOne( model.CollectionTable );
					model.Element = element;
					BindManyToOne( subnode, element, Mapping.Collection.DefaultElementColumnName, false, mappings );
				}
				else if( "composite-element".Equals( name ) )
				{
					Component element = new Component( model.CollectionTable );
					model.Element = element;
					BindComponent( subnode, element, null, model.Role, "element", true, mappings );
				}
				else if( "many-to-any".Equals( name ) )
				{
					Any element = new Any( model.CollectionTable );
					model.Element = element;
					BindAny( subnode, element, true, mappings );
				}
				else if( "jcs-cache".Equals( name ) || "cache".Equals( name ) )
				{
					ICacheConcurrencyStrategy cache = CacheFactory.CreateCache( subnode, model.Role, model.Owner.IsMutable );
					mappings.AddCache( model.Role, cache );
					model.Cache = cache;
				}
			}

			// Code below is not present in H2.1, why was it added?
			if( !model.IsInverse )
			{
				if( !model.IsOneToMany ) // no foreign key for a one-to-many
				{
					model.Element.CreateForeignKey();
				}

				model.Key.CreateForeignKeyOfClass( model.Owner.MappedClass );
			}
		}
Exemplo n.º 8
0
		/// <summary>
		/// Called for Maps
		/// </summary>
		/// <param name="node"></param>
		/// <param name="model"></param>
		/// <param name="classes"></param>
		/// <param name="mappings"></param>
		public static void BindMapSecondPass( XmlNode node, Map model, IDictionary classes, Mappings mappings )
		{
			BindCollectionSecondPass( node, model, classes, mappings );

			foreach( XmlNode subnode in node.ChildNodes )
			{
				//I am only concerned with elements that are from the nhibernate namespace
				if( subnode.NamespaceURI != Configuration.MappingSchemaXMLNS )
				{
					continue;
				}

				string name = subnode.LocalName; //.Name;

				if( "index".Equals( name ) )
				{
					SimpleValue value = new SimpleValue( model.CollectionTable );
					BindSimpleValue( subnode, value, model.IsOneToMany, IndexedCollection.DefaultIndexColumnName, mappings );
					model.Index = value;
					if( model.Index.Type == null )
					{
						throw new MappingException( "map index element must specify a type: " + model.Role );
					}
				}
				else if( "index-many-to-many".Equals( name ) )
				{
					ManyToOne mto = new ManyToOne( model.CollectionTable );
					BindManyToOne( subnode, mto, IndexedCollection.DefaultIndexColumnName, model.IsOneToMany, mappings );
					model.Index = mto;
				}
				else if( "composite-index".Equals( name ) )
				{
					Component component = new Component( model.CollectionTable );
					BindComponent( subnode, component, null, model.Role, "index", model.IsOneToMany, mappings );
					model.Index = component;
				}
				else if( "index-many-to-any".Equals( name ) )
				{
					Any any = new Any( model.CollectionTable );
					BindAny( subnode, any, model.IsOneToMany, mappings );
					model.Index = any;
				}
			}
		}
Exemplo n.º 9
0
		protected static void PropertiesFromXML( XmlNode node, PersistentClass model, Mappings mappings )
		{
			Table table = model.Table;

			foreach( XmlNode subnode in node.ChildNodes )
			{
				//I am only concerned with elements that are from the nhibernate namespace
				if( subnode.NamespaceURI != Configuration.MappingSchemaXMLNS )
				{
					continue;
				}

				string name = subnode.LocalName; //.Name;
				string propertyName = GetPropertyName( subnode );

				CollectionType collectType = CollectionType.CollectionTypeFromString( name );
				IValue value = null;
				if( collectType != null )
				{
					Mapping.Collection collection = collectType.Create( subnode, model.Name, propertyName, model, mappings );
					mappings.AddCollection( collection );
					value = collection;
				}
				else if( "many-to-one".Equals( name ) )
				{
					value = new ManyToOne( table );
					BindManyToOne( subnode, ( ManyToOne ) value, propertyName, true, mappings );
				}
				else if( "any".Equals( name ) )
				{
					value = new Any( table );
					BindAny( subnode, ( Any ) value, true, mappings );
				}
				else if( "one-to-one".Equals( name ) )
				{
					value = new OneToOne( table, model.Identifier );
					BindOneToOne( subnode, ( OneToOne ) value, true, mappings );
				}
				else if( "property".Equals( name ) )
				{
					value = new SimpleValue( table );
					BindSimpleValue( subnode, ( SimpleValue ) value, true, propertyName, mappings );
				}
				else if( "component".Equals( name ) || "dynamic-component".Equals( name ) )
				{
					// NH: Modified from H2.1 to allow specifying the type explicitly using class attribute
					System.Type reflectedClass = GetPropertyType( subnode, mappings, model.MappedClass, propertyName );
					value = new Component( model );
					BindComponent( subnode, ( Component ) value, reflectedClass, model.Name, propertyName, true, mappings );
				}
				else if( "subclass".Equals( name ) )
				{
					HandleSubclass( model, mappings, subnode );
				}
				else if( "joined-subclass".Equals( name ) )
				{
					HandleJoinedSubclass( model, mappings, subnode );
				}
				if( value != null )
				{
					model.AddNewProperty( CreateProperty( value, propertyName, model.MappedClass, subnode, mappings ) );
				}
			}
		}
Exemplo n.º 10
0
        private void BindJoin(XmlNode node, Join join)
        {
            PersistentClass persistentClass = join.PersistentClass;
            String path = persistentClass.EntityName;

            // TABLENAME

            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;

            Table table = mappings.AddTable(schema, catalog, GetClassTableName(persistentClass, node), null, false);
            join.Table = table;

            XmlAttribute fetchNode = node.Attributes["fetch"];
            if (fetchNode != null)
                join.IsSequentialSelect = "select".Equals(fetchNode.Value);

            XmlAttribute invNode = node.Attributes["inverse"];
            if (invNode != null)
                join.IsInverse = "true".Equals(invNode.Value);

            XmlAttribute nullNode = node.Attributes["optional"];
            if (nullNode != null)
                join.IsOptional = "true".Equals(nullNode.Value);

            log.InfoFormat("Mapping class join: {0} -> {1}", persistentClass.EntityName, join.Table.Name);

            // KEY
            XmlNode keyNode = node.SelectSingleNode(HbmConstants.nsKey, namespaceManager);
            SimpleValue key = new DependantValue(table, persistentClass.Identifier);
            join.Key = key;
            if (keyNode.Attributes["on-delete"] != null)
                key.IsCascadeDeleteEnabled = "cascade".Equals(keyNode.Attributes["on-delete"].Value);
            BindSimpleValue(keyNode, key, false, persistentClass.EntityName);

            join.CreatePrimaryKey(dialect);
            join.CreateForeignKey();

            // PROPERTIES
            //PropertiesFromXML(node, persistentClass, mappings);
            foreach (XmlNode subnode in node.ChildNodes)
            {
                string name = subnode.Name;
                XmlAttribute nameAttribute = subnode.Attributes["name"];
                string propertyName = nameAttribute == null ? null : nameAttribute.Value;

                IValue value = null;
                switch (name)
                {
                    case "many-to-one":
                        value = new ManyToOne(table);
                        BindManyToOne(subnode, (ManyToOne) value, propertyName, true);
                        break;
                    case "any":
                        value = new Any(table);
                        BindAny(subnode, (Any) value, true);
                        break;
                    case "property":
                        value = new SimpleValue(table);
                        BindSimpleValue(subnode, (SimpleValue) value, true, propertyName);
                        break;
                    case "component":
                    case "dynamic-component":
                        string subpath = StringHelper.Qualify(path, propertyName);
                        value = new Component(join);
                        BindComponent(
                            subnode,
                            (Component) value,
                            join.PersistentClass.MappedClass,
                            propertyName,
                            subpath,
                            true);
                        break;
                }

                if (value != null)
                {
                    Mapping.Property prop = CreateProperty(value, propertyName, persistentClass.MappedClass, subnode);
                    prop.IsOptional = join.IsOptional;
                    join.AddProperty(prop);
                }
            }

            // CUSTOM SQL
            HandleCustomSQL(node, join);
        }
Exemplo n.º 11
0
		/// <remarks>
		/// Called for all collections
		/// </remarks>
		private void BindCollectionSecondPass(XmlNode node, Mapping.Collection model,
			IDictionary<string, PersistentClass> persistentClasses)
		{
			if (model.IsOneToMany)
			{
				OneToMany 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
			XmlAttribute chNode = node.Attributes["check"];
			if (chNode != null)
				model.CollectionTable.AddCheckConstraint(chNode.Value);

			//contained elements:
			foreach (XmlNode subnode in node.ChildNodes)
			{
				//I am only concerned with elements that are from the nhibernate namespace
				if (subnode.NamespaceURI != Configuration.MappingSchemaXMLNS)
					continue;

				string name = subnode.LocalName; //.Name;

				if ("key".Equals(name) || "generated-key".Equals(name))
				{
					string propRef = model.ReferencedPropertyName;
					IKeyValue keyValue;
					if (propRef == null)
					{
						keyValue = model.Owner.Identifier;
					}
					else
					{
						keyValue = (IKeyValue)model.Owner.GetProperty(propRef).Value;
					}
					DependantValue key = new DependantValue(model.CollectionTable, keyValue);
					if (subnode.Attributes["on-delete"] != null)
						key.IsCascadeDeleteEnabled = "cascade".Equals(subnode.Attributes["on-delete"].Value);
					BindSimpleValue(subnode, key, model.IsOneToMany, Mapping.Collection.DefaultKeyColumnName);
					if (key.Type.ReturnedClass.IsArray)
						throw new MappingException("illegal use of an array as an identifier (arrays don't reimplement Equals)");
					model.Key = key;


					XmlAttribute notNull = subnode.Attributes["not-null"];
					key.SetNullable(notNull == null || IsFalse(notNull.Value));
					XmlAttribute updateable = subnode.Attributes["update"];
					key.SetUpdateable(updateable == null || IsTrue(updateable.Value));
				}
				else if ("element".Equals(name))
				{
					SimpleValue elt = new SimpleValue(model.CollectionTable);
					model.Element = elt;
					BindSimpleValue(subnode, elt, true, Mapping.Collection.DefaultElementColumnName);
				}
				else if ("many-to-many".Equals(name))
				{
					ManyToOne element = new ManyToOne(model.CollectionTable);
					model.Element = element;
					BindManyToOne(subnode, element, Mapping.Collection.DefaultElementColumnName, false);
					BindManyToManySubelements(model, subnode);
				}
				else if ("composite-element".Equals(name))
				{
					Component element = new Component(model);
					model.Element = element;
					BindComponent(subnode, element, null, model.Role, "element", true);
				}
				else if ("many-to-any".Equals(name))
				{
					Any element = new Any(model.CollectionTable);
					model.Element = element;
					BindAny(subnode, element, true);
				}
				else if ("jcs-cache".Equals(name) || "cache".Equals(name))
				{
					XmlAttribute usageNode = subnode.Attributes["usage"];
					model.CacheConcurrencyStrategy = (usageNode != null) ? usageNode.Value : null;
					XmlAttribute regionNode = subnode.Attributes["region"];
					model.CacheRegionName = (regionNode != null) ? regionNode.Value : null;
				}
			}
		}
Exemplo n.º 12
0
		/// <summary>
		/// Called for Maps
		/// </summary>
		private void BindMapSecondPass(HbmMap mapMapping, Map model,
			IDictionary<string, PersistentClass> persistentClasses, IDictionary<string, MetaAttribute> inheritedMetas)
		{
			BindCollectionSecondPass(mapMapping, model, persistentClasses, inheritedMetas);

			HbmIndex indexMapping;
			HbmMapKey mapKeyMapping;

			HbmIndexManyToMany indexManyToManyMapping;
			HbmMapKeyManyToMany mapKeyManyToManyMapping;

			HbmCompositeIndex compositeIndexMapping;
			HbmCompositeMapKey compositeMapKeyMapping;

			HbmIndexManyToAny indexManyToAnyMapping;

			if ((indexMapping = mapMapping.Item as HbmIndex) != null)
			{
				var value = new SimpleValue(model.CollectionTable);
				new ValuePropertyBinder(value, Mappings).BindSimpleValue(indexMapping, IndexedCollection.DefaultIndexColumnName,
																		 model.IsOneToMany);
				model.Index = value;
				if (string.IsNullOrEmpty(model.Index.TypeName))
					throw new MappingException("map index element must specify a type: " + model.Role);
			}
			else if ((mapKeyMapping = mapMapping.Item as HbmMapKey) != null)
			{
				var value = new SimpleValue(model.CollectionTable);
				new ValuePropertyBinder(value, Mappings).BindSimpleValue(mapKeyMapping, IndexedCollection.DefaultIndexColumnName,
																																 model.IsOneToMany);
				model.Index = value;
				if (string.IsNullOrEmpty(model.Index.TypeName))
					throw new MappingException("map index element must specify a type: " + model.Role);
			}
			else if ((indexManyToManyMapping = mapMapping.Item as HbmIndexManyToMany) != null)
			{
				var manyToOne = new ManyToOne(model.CollectionTable);
				BindIndexManyToMany(indexManyToManyMapping, manyToOne, IndexedCollection.DefaultIndexColumnName, model.IsOneToMany);
				model.Index = manyToOne;
			}
			else if ((mapKeyManyToManyMapping = mapMapping.Item as HbmMapKeyManyToMany) != null)
			{
				var manyToOne = new ManyToOne(model.CollectionTable);
				BindMapKeyManyToMany(mapKeyManyToManyMapping, manyToOne, IndexedCollection.DefaultIndexColumnName, model.IsOneToMany);
				model.Index = manyToOne;
			}
			else if ((compositeIndexMapping = mapMapping.Item as HbmCompositeIndex) != null)
			{
				var component = new Component(model);
				BindComponent(compositeIndexMapping, component, null, null, model.Role + ".index", model.IsOneToMany, inheritedMetas);
				model.Index = component;
			}
			else if ((compositeMapKeyMapping = mapMapping.Item as HbmCompositeMapKey) != null)
			{
				var component = new Component(model);
				BindComponent(compositeMapKeyMapping, component, null, null, model.Role + ".index", model.IsOneToMany, inheritedMetas);
				model.Index = component;
			}
			else if ((indexManyToAnyMapping = mapMapping.Item as HbmIndexManyToAny) != null)
			{
				var any = new Any(model.CollectionTable);
				BindIndexManyToAny(indexManyToAnyMapping, any, model.IsOneToMany);
				model.Index = any;				
			}

			bool indexIsFormula = model.Index.ColumnIterator.Any(x=> x.IsFormula);
			if (NeedBackref(model) && !indexIsFormula)
			{
				string entityName = ((OneToMany)model.Element).ReferencedEntityName;
				PersistentClass referenced = mappings.GetClass(entityName);
				var ib = new IndexBackref();
				ib.Name = '_' + model.OwnerEntityName + "." + mapMapping.Name + "IndexBackref";
				ib.IsUpdateable = false;
				ib.IsSelectable = false;
				ib.CollectionRole = model.Role;
				ib.EntityName = model.Owner.EntityName;
				ib.Value = model.Index;
				referenced.AddProperty(ib);
			}
		}
		    public ComponentPropertiesSource(Component component) {
			    try {                    
                    this.xclass = component.ComponentClass;

				    //this.xclass = reflectionManager.classForName(component.getComponentClassName(), this.getClass());
			    } catch (Exception e) {
				    throw new MappingException(e);
			    }

			    this.component = component;
		    }
Exemplo n.º 14
0
		private void BindComponentProperty(HbmComponent componentMapping, Property property, Component model)
		{
			property.IsUpdateable = componentMapping.update;
			property.IsInsertable = componentMapping.insert;
			if (componentMapping.unique)
			{
				model.Owner.Table.CreateUniqueKey(model.ColumnIterator.OfType<Column>().ToList());
			}
			HbmTuplizer[] tuplizers = componentMapping.tuplizer;
			if (tuplizers != null)
			{
				Array.ForEach(tuplizers.Select(tuplizer => new
				                                           	{
				                                           		TuplizerClassName = FullQualifiedClassName(tuplizer.@class, mappings),
				                                           		Mode = tuplizer.entitymode.ToEntityMode()
				                                           	}).ToArray(),
				              x => model.AddTuplizer(x.Mode, x.TuplizerClassName));
			}
		}
Exemplo n.º 15
0
		private void BindComponentProperty(HbmProperties propertiesMapping, Property property, Component model)
		{
			property.IsUpdateable = propertiesMapping.update;
			property.IsInsertable = propertiesMapping.insert;
			if (propertiesMapping.unique)
			{
				model.Owner.Table.CreateUniqueKey(model.ColumnIterator.OfType<Column>().ToList());
			}
		}
		private bool HasPartialUpdateComponentGeneration(Component component)
		{
			foreach (Mapping.Property prop in component.PropertyCollection)
			{
				if (prop.Generation == PropertyGeneration.Always)
				{
					return true;
				}
				else if (prop.Value is Component)
				{
					if (HasPartialUpdateComponentGeneration((Component) prop.Value))
					{
						return true;
					}
				}
			}
			return false;
		}
Exemplo n.º 17
0
		private static RootClass CreateMappingClasses()
		{
			var classMapping = new RootClass();
			var componentMapping = new NHibernate.Mapping.Component(classMapping);

			var componentPropertyMapping = new Property(componentMapping);
			componentPropertyMapping.Name = "ComponentPropertyInClass";
			classMapping.AddProperty(componentPropertyMapping);

			var stringValue = new SimpleValue();
			stringValue.TypeName = typeof(string).FullName;

			var stringPropertyInComponentMapping = new Property(stringValue);
			stringPropertyInComponentMapping.Name = "StringPropertyInComponent";
			componentMapping.AddProperty(stringPropertyInComponentMapping);

			var componentType = (IAbstractComponentType)componentMapping.Type;

			Assume.That(CascadeStyle.None == stringPropertyInComponentMapping.CascadeStyle);
			Assume.That(CascadeStyle.None == componentType.GetCascadeStyle(0));
			Assume.That(CascadeStyle.None == componentPropertyMapping.CascadeStyle);

			return classMapping;
		}
Exemplo n.º 18
0
		public static void BindRootClass( XmlNode node, RootClass model, Mappings mappings )
		{
			BindClass( node, model, mappings );

			//TABLENAME
			XmlAttribute schemaNode = node.Attributes[ "schema" ];
			string schema = schemaNode == null ? mappings.SchemaName : schemaNode.Value;
			Table table = mappings.AddTable( schema, GetClassTableName( model, node, mappings ) );
			model.Table = table;

			log.Info( "Mapping class: " + model.Name + " -> " + model.Table.Name );

			//MUTABLE
			XmlAttribute mutableNode = node.Attributes[ "mutable" ];
			model.IsMutable = ( mutableNode == null ) || mutableNode.Value.Equals( "true" );

			//WHERE
			XmlAttribute whereNode = node.Attributes[ "where" ];
			if( whereNode != null )
			{
				model.Where = whereNode.Value;
			}

			//CHECK
			XmlAttribute checkNode = node.Attributes[ "check" ];
			if( checkNode != null )
			{
				table.AddCheckConstraint( checkNode.Value );
			}

			//POLYMORPHISM
			XmlAttribute polyNode = node.Attributes[ "polymorphism" ];
			model.IsExplicitPolymorphism = ( polyNode != null ) && polyNode.Value.Equals( "explicit" );

			foreach( XmlNode subnode in node.ChildNodes )
			{
				string name = subnode.LocalName; //Name;
				string propertyName = GetPropertyName( subnode );

				//I am only concerned with elements that are from the nhibernate namespace
				if( subnode.NamespaceURI != Configuration.MappingSchemaXMLNS )
				{
					continue;
				}

				switch( name )
				{
				case "id":
					SimpleValue id = new SimpleValue( table );
					model.Identifier = id;

					if( propertyName == null )
					{
						BindSimpleValue( subnode, id, false, RootClass.DefaultIdentifierColumnName, mappings );
						if( id.Type == null )
						{
							throw new MappingException( "must specify an identifier type: " + model.MappedClass.Name );
						}
						model.IdentifierProperty = null;
					}
					else
					{
						BindSimpleValue( subnode, id, false, propertyName, mappings );
						id.SetTypeByReflection( model.MappedClass, propertyName, PropertyAccess( subnode, mappings ) );
						Mapping.Property prop = new Mapping.Property( id );
						BindProperty( subnode, prop, mappings );
						model.IdentifierProperty = prop;
					}

					if( id.Type.ReturnedClass.IsArray )
					{
						throw new MappingException( "illegal use of an array as an identifier (arrays don't reimplement equals)" );
					}

					MakeIdentifier( subnode, id, mappings );
					break;

				case "composite-id":
					Component compId = new Component( model );
					model.Identifier = compId;
					if( propertyName == null )
					{
						BindComponent( subnode, compId, null, model.Name, "id", false, mappings );
						model.HasEmbeddedIdentifier = compId.IsEmbedded;
						model.IdentifierProperty = null;
					}
					else
					{
						System.Type reflectedClass = GetPropertyType( subnode, mappings, model.MappedClass, propertyName );
						BindComponent( subnode, compId, reflectedClass, model.Name, propertyName, false, mappings );
						Mapping.Property prop = new Mapping.Property( compId );
						BindProperty( subnode, prop, mappings );
						model.IdentifierProperty = prop;
					}
					MakeIdentifier( subnode, compId, mappings );

					System.Type compIdClass = compId.ComponentClass;
					if( !ReflectHelper.OverridesEquals( compIdClass ) )
					{
						throw new MappingException(
							"composite-id class must override Equals(): " + compIdClass.FullName
							);
					}

					if( !ReflectHelper.OverridesGetHashCode( compIdClass ) )
					{
						throw new MappingException(
							"composite-id class must override GetHashCode(): " + compIdClass.FullName
							);
					}

					// Serializability check not ported
					break;

				case "version":
				case "timestamp":
					//VERSION
					SimpleValue val = new SimpleValue( table );
					BindSimpleValue( subnode, val, false, propertyName, mappings );
					if( val.Type == null )
					{
						val.Type = ( ( "version".Equals( name ) ) ? NHibernateUtil.Int32 : NHibernateUtil.Timestamp );
					}
					Mapping.Property timestampProp = new Mapping.Property( val );
					BindProperty( subnode, timestampProp, mappings );
					MakeVersion( subnode, val );
					model.Version = timestampProp;
					model.AddNewProperty( timestampProp );
					break;

				case "discriminator":
					//DISCRIMINATOR
					SimpleValue discrim = new SimpleValue( table );
					model.Discriminator = discrim;
					BindSimpleValue( subnode, discrim, false, RootClass.DefaultDiscriminatorColumnName, mappings );
					if( discrim.Type == null )
					{
						discrim.Type = NHibernateUtil.String;
						foreach( Column col in discrim.ColumnCollection )
						{
							col.Type = NHibernateUtil.String;
							break;
						}
					}
					model.IsPolymorphic = true;
					if( subnode.Attributes[ "force" ] != null && "true".Equals( subnode.Attributes[ "force" ].Value ) )
					{
						model.IsForceDiscriminator = true;
					}
					if( subnode.Attributes[ "insert" ] != null && "false".Equals( subnode.Attributes[ "insert" ].Value ) )
					{
						model.IsDiscriminatorInsertable = false;
					}
					break;

				case "jcs-cache":
				case "cache":
					string className = model.MappedClass.FullName;
					ICacheConcurrencyStrategy cache = CacheFactory.CreateCache( subnode, className, model.IsMutable );
					mappings.AddCache( className, cache );
					model.Cache = cache;

					break;
				}
			}

			model.CreatePrimaryKey( dialect );

			PropertiesFromXML( node, model, mappings );
		}
		protected void BindComponent(IComponentMapping componentMapping, Component model, System.Type reflectedClass, string className,
		                             string path, bool isNullable,
		                             IDictionary<string, MetaAttribute> inheritedMetas)
		{
			model.RoleName = path;
			inheritedMetas = GetMetas(componentMapping as IDecoratable, inheritedMetas);
			model.MetaAttributes = inheritedMetas;
			var componentClassName = componentMapping.Class;
			if (!string.IsNullOrEmpty(componentClassName))
			{
				model.ComponentClass = ClassForNameChecked(componentClassName, mappings, "component class not found: {0}");
				model.ComponentClassName = FullQualifiedClassName(componentClassName, mappings);
				model.IsEmbedded = false;
			}
			else if (componentMapping is HbmDynamicComponent)
			{
				model.IsEmbedded = false;
				model.IsDynamic = true;
			}

			else if (reflectedClass != null)
			{
				model.ComponentClass = reflectedClass;
				model.IsEmbedded = false;
			}
			else
			{
				// an "embedded" component (ids only)
				model.IsEmbedded = true;
				if (model.Owner.HasPocoRepresentation)
				{
					model.ComponentClass = model.Owner.MappedClass;
				}
				else
				{
					model.IsDynamic = true;
				}
			}

			string nodeName = !string.IsNullOrEmpty(componentMapping.EmbeddedNode)
			                  	? componentMapping.EmbeddedNode
			                  	: !string.IsNullOrEmpty(componentMapping.Name) ? componentMapping.Name : model.Owner.NodeName;
			model.NodeName = nodeName;

			// Parent
			if (componentMapping.Parent != null && !string.IsNullOrEmpty(componentMapping.Parent.name))
			{
				model.ParentProperty = new Property
				                       	{
				                       		Name = componentMapping.Parent.name,
				                       		PropertyAccessorName = componentMapping.Parent.access ?? mappings.DefaultAccess
				                       	}; 
			}

			new PropertiesBinder(Mappings, model, className, path, isNullable, Mappings.Dialect).Bind(
				componentMapping.Properties, model.Table, inheritedMetas, p =>
					{ }, model.AddProperty);
		}
Exemplo n.º 20
0
		public static void BindComponent( XmlNode node, Component model, System.Type reflectedClass, string className, string path, bool isNullable, Mappings mappings )
		{
			XmlAttribute classNode = node.Attributes[ "class" ];

			if( "dynamic-component".Equals( node.Name ) )
			{
				model.IsEmbedded = false;
				model.IsDynamic = true;
			}
			else if( classNode != null )
			{
				model.ComponentClass = ClassForNameChecked(
					classNode.Value, mappings,
					"component class not found: {0}" );
				model.IsEmbedded = false;
			}
			else if( reflectedClass != null )
			{
				model.ComponentClass = reflectedClass;
				model.IsEmbedded = false;
			}
			else
			{
				// an "embedded" component (ids only)
				model.ComponentClass = model.Owner.MappedClass;
				model.IsEmbedded = true;
			}

			foreach( XmlNode subnode in node.ChildNodes )
			{
				//I am only concerned with elements that are from the nhibernate namespace
				if( subnode.NamespaceURI != Configuration.MappingSchemaXMLNS )
				{
					continue;
				}

				string name = subnode.LocalName; //.Name;
				string propertyName = GetPropertyName( subnode );
				string subpath = propertyName == null ? null : StringHelper.Qualify( path, propertyName );

				CollectionType collectType = CollectionType.CollectionTypeFromString( name );
				IValue value = null;
				if( collectType != null )
				{
					Mapping.Collection collection = collectType.Create( subnode, className, subpath, model.Owner, mappings );
					mappings.AddCollection( collection );
					value = collection;
				}
				else if( "many-to-one".Equals( name ) || "key-many-to-one".Equals( name ) )
				{
					value = new ManyToOne( model.Table );
					BindManyToOne( subnode, ( ManyToOne ) value, subpath, isNullable, mappings );
				}
				else if( "one-to-one".Equals( name ) )
				{
					value = new OneToOne( model.Table, model.Owner.Identifier );
					BindOneToOne( subnode, ( OneToOne ) value, isNullable, mappings );
				}
				else if( "any".Equals( name ) )
				{
					value = new Any( model.Table );
					BindAny( subnode, ( Any ) value, isNullable, mappings );
				}
				else if( "property".Equals( name ) || "key-property".Equals( name ) )
				{
					value = new SimpleValue( model.Table );
					BindSimpleValue( subnode, ( SimpleValue ) value, isNullable, subpath, mappings );
				}
				else if( "component".Equals( name ) || "dynamic-component".Equals( name ) || "nested-composite-element".Equals( name ) )
				{
					System.Type subreflectedClass = model.ComponentClass == null ?
						null :
						GetPropertyType( subnode, mappings, model.ComponentClass, propertyName );
					value = ( model.Owner != null ) ?
						new Component( model.Owner ) : // a class component
						new Component( model.Table ); // a composite element
					BindComponent( subnode, ( Component ) value, subreflectedClass, className, subpath, isNullable, mappings );
				}
				else if( "parent".Equals( name ) )
				{
					model.ParentProperty = propertyName;
				}

				if( value != null )
				{
					model.AddProperty( CreateProperty( value, propertyName, model.ComponentClass, subnode, mappings ) );
				}
			}

			int span = model.PropertySpan;
			string[ ] names = new string[span];
			IType[ ] types = new IType[span];
			Cascades.CascadeStyle[ ] cascade = new Cascades.CascadeStyle[span];
			OuterJoinFetchStrategy[ ] joinedFetch = new OuterJoinFetchStrategy[span];

			int i = 0;
			foreach( Mapping.Property prop in model.PropertyCollection )
			{
				if( prop.IsFormula )
				{
					throw new MappingException( "properties of components may not be formulas: " + prop.Name );
				}
				if( !prop.IsInsertable || !prop.IsUpdateable )
				{
					throw new MappingException( "insert=\"false\", update=\"false\" not supported for properties of components: " + prop.Name );
				}
				names[ i ] = prop.Name;
				types[ i ] = prop.Type;
				cascade[ i ] = prop.CascadeStyle;
				joinedFetch[ i ] = prop.Value.OuterJoinFetchSetting;
				i++;
			}

			IType componentType;
			if( model.IsDynamic )
			{
				componentType = new DynamicComponentType( names, types, joinedFetch, cascade );
			}
			else
			{
				IGetter[ ] getters = new IGetter[span];
				ISetter[ ] setters = new ISetter[span];
				bool foundCustomAccessor = false;
				i = 0;
				foreach( Mapping.Property prop in model.PropertyCollection )
				{
					setters[ i ] = prop.GetSetter( model.ComponentClass );
					getters[ i ] = prop.GetGetter( model.ComponentClass );
					if( !prop.IsBasicPropertyAccessor )
					{
						foundCustomAccessor = true;
					}
					i++;
				}

				componentType = new ComponentType(
					model.ComponentClass,
					names,
					getters,
					setters,
					foundCustomAccessor,
					types,
					joinedFetch,
					cascade,
					model.ParentProperty );
			}
			model.Type = componentType;
		}
Exemplo n.º 21
0
		protected void PropertiesFromXML(XmlNode node, PersistentClass model, IDictionary<string, MetaAttribute> inheritedMetas, UniqueKey uniqueKey, bool mutable, bool nullable, bool naturalId)
		{
			string entityName = model.EntityName;

			Table table = model.Table;

			foreach (XmlNode subnode in node.ChildNodes)
			{
				//I am only concerned with elements that are from the nhibernate namespace
				if (subnode.NamespaceURI != Configuration.MappingSchemaXMLNS)
					continue;

				string name = subnode.LocalName; //.Name;
				string propertyName = GetPropertyName(subnode);

				IValue value = null;
				CollectionBinder collectionBinder = new CollectionBinder(this);
				if (collectionBinder.CanCreate(name))
				{
					Mapping.Collection collection = collectionBinder.Create(name, subnode, entityName, propertyName, model,
					                                                        model.MappedClass, inheritedMetas);

					mappings.AddCollection(collection);
					value = collection;
				}
				else if ("many-to-one".Equals(name))
				{
					value = new ManyToOne(table);
					BindManyToOne(subnode, (ManyToOne) value, propertyName, true);
				}
				else if ("any".Equals(name))
				{
					value = new Any(table);
					BindAny(subnode, (Any) value, true);
				}
				else if ("one-to-one".Equals(name))
				{
					value = new OneToOne(table, model);
					BindOneToOne(subnode, (OneToOne) value);
				}
				else if ("property".Equals(name))
				{
					value = new SimpleValue(table);
					BindSimpleValue(subnode, (SimpleValue) value, true, propertyName);
				}
				else if ("component".Equals(name) || "dynamic-component".Equals(name))
				{
					string subpath = StringHelper.Qualify(entityName, propertyName);
					// NH: Modified from H2.1 to allow specifying the type explicitly using class attribute
					System.Type reflectedClass = GetPropertyType(subnode, model.MappedClass, propertyName);
					value = new Component(model);
					BindComponent(subnode, (Component) value, reflectedClass, entityName, propertyName, subpath, true, inheritedMetas);
				}
				else if ("join".Equals(name))
				{
					Join join = new Join();
					join.PersistentClass = model;
					BindJoin(subnode, join, inheritedMetas);
					model.AddJoin(join);
				}
				else if ("subclass".Equals(name))
					new SubclassBinder(this).HandleSubclass(model, subnode, inheritedMetas);

				else if ("joined-subclass".Equals(name))
					new JoinedSubclassBinder(this).HandleJoinedSubclass(model, subnode, inheritedMetas);

				else if ("union-subclass".Equals(name))
					new UnionSubclassBinder(this).HandleUnionSubclass(model, subnode, inheritedMetas);

				else if ("filter".Equals(name))
					ParseFilter(subnode, model);
				else if ("natural-id".Equals(name))
				{
					UniqueKey uk = new UniqueKey();
					uk.Name = "_UniqueKey";
					uk.Table = table;
					//by default, natural-ids are "immutable" (constant)

					bool mutableId = false;
					if (subnode.Attributes["mutable"] != null)
					{
						mutableId = "true".Equals(subnode.Attributes["mutable"]);						
					}

					PropertiesFromXML(subnode, model, inheritedMetas, uk, mutableId, false, true);
					table.AddUniqueKey(uk);
				}

				if (value != null)
				{
					Property property = CreateProperty(value, propertyName, model.ClassName, subnode, inheritedMetas);
					if (!mutable)
						property.IsUpdateable = false;
					if (naturalId)
						property.IsNaturalIdentifier = true;
					model.AddProperty(property);
					if (uniqueKey != null)
						uniqueKey.AddColumns(new SafetyEnumerable<Column>(property.ColumnIterator));
				}
			}
		}
Exemplo n.º 22
0
		private void BindCompositeElement(HbmCompositeElement compositeElementMapping, Mapping.Collection model, IDictionary<string, MetaAttribute> inheritedMetas)
		{
			var component = new Component(model);
			model.Element = component;
			BindComponent(compositeElementMapping, component, null, null, model.Role + ".element", true, inheritedMetas);
		}
Exemplo n.º 23
0
        protected void PropertiesFromXML(XmlNode node, PersistentClass model)
        {
            Table table = model.Table;

            foreach (XmlNode subnode in node.ChildNodes)
            {
                //I am only concerned with elements that are from the nhibernate namespace
                if (subnode.NamespaceURI != Configuration.MappingSchemaXMLNS)
                    continue;

                string name = subnode.LocalName; //.Name;
                string propertyName = GetPropertyName(subnode);

                IValue value = null;
                CollectionBinder collectionBinder = new CollectionBinder(this);
                if (collectionBinder.CanCreate(name))
                {
                    Mapping.Collection collection = collectionBinder.Create(name, subnode, model.EntityName,
                        propertyName, model, model.MappedClass);

                    mappings.AddCollection(collection);
                    value = collection;
                }
                else if ("many-to-one".Equals(name))
                {
                    value = new ManyToOne(table);
                    BindManyToOne(subnode, (ManyToOne) value, propertyName, true);
                }
                else if ("any".Equals(name))
                {
                    value = new Any(table);
                    BindAny(subnode, (Any) value, true);
                }
                else if ("one-to-one".Equals(name))
                {
                    value = new OneToOne(table, model);
                    BindOneToOne(subnode, (OneToOne) value);
                }
                else if ("property".Equals(name))
                {
                    value = new SimpleValue(table);
                    BindSimpleValue(subnode, (SimpleValue) value, true, propertyName);
                }
                else if ("component".Equals(name) || "dynamic-component".Equals(name))
                {
                    // NH: Modified from H2.1 to allow specifying the type explicitly using class attribute
                    System.Type reflectedClass = GetPropertyType(subnode, model.MappedClass, propertyName);
                    value = new Component(model);
                    BindComponent(subnode, (Component) value, reflectedClass, model.EntityName, propertyName, true);
                }
                else if ("join".Equals(name))
                {
                    Join join = new Join();
                    join.PersistentClass = model;
                    BindJoin(subnode, join);
                    model.AddJoin(join);
                }
                else if ("subclass".Equals(name))
                    new SubclassBinder(this).HandleSubclass(model, subnode);

                else if ("joined-subclass".Equals(name))
                    new JoinedSubclassBinder(this).HandleJoinedSubclass(model, subnode);

                else if ("union-subclass".Equals(name))
                    new UnionSubclassBinder(this).HandleUnionSubclass(model, subnode);

                else if ("filter".Equals(name))
                    ParseFilter(subnode, model);

                if (value != null)
                    model.AddProperty(CreateProperty(value, propertyName, model.MappedClass, subnode));
            }
        }
Exemplo n.º 24
0
        protected void BindComponent(XmlNode node, Component model, System.Type reflectedClass,
			string className, string path, bool isNullable)
        {
            XmlAttribute classNode = node.Attributes["class"];

            if ("dynamic-component".Equals(node.Name))
            {
                model.IsEmbedded = false;
                model.IsDynamic = true;
            }
            else if (classNode != null)
            {
                model.ComponentClass = ClassForNameChecked(
                    classNode.Value, mappings,
                    "component class not found: {0}");
                model.ComponentClassName = FullClassName(classNode.Value, mappings);
                model.IsEmbedded = false;
            }
            else if (reflectedClass != null)
            {
                model.ComponentClass = reflectedClass;
                model.IsEmbedded = false;
            }
            else
            {
                // an "embedded" component (ids only)
                model.ComponentClass = model.Owner.MappedClass;
                model.IsEmbedded = true;
            }

            foreach (XmlNode subnode in node.ChildNodes)
            {
                //I am only concerned with elements that are from the nhibernate namespace
                if (subnode.NamespaceURI != Configuration.MappingSchemaXMLNS)
                    continue;

                string name = subnode.LocalName; //.Name;
                string propertyName = GetPropertyName(subnode);
                string subpath = propertyName == null ? null : StringHelper.Qualify(path, propertyName);

                IValue value = null;

                CollectionBinder binder = new CollectionBinder(this);

                if (binder.CanCreate(name))
                {
                    Mapping.Collection collection = binder.Create(name, subnode, className,
                        subpath, model.Owner, model.ComponentClass);

                    mappings.AddCollection(collection);
                    value = collection;
                }
                else if ("many-to-one".Equals(name) || "key-many-to-one".Equals(name))
                {
                    value = new ManyToOne(model.Table);
                    BindManyToOne(subnode, (ManyToOne) value, subpath, isNullable);
                }
                else if ("one-to-one".Equals(name))
                {
                    value = new OneToOne(model.Table, model.Owner);
                    BindOneToOne(subnode, (OneToOne) value);
                }
                else if ("any".Equals(name))
                {
                    value = new Any(model.Table);
                    BindAny(subnode, (Any) value, isNullable);
                }
                else if ("property".Equals(name) || "key-property".Equals(name))
                {
                    value = new SimpleValue(model.Table);
                    BindSimpleValue(subnode, (SimpleValue) value, isNullable, subpath);
                }
                else if ("component".Equals(name) || "dynamic-component".Equals(name) || "nested-composite-element".Equals(name))
                {
                    System.Type subreflectedClass = model.ComponentClass == null
                        ?
                            null
                        :
                            GetPropertyType(subnode, model.ComponentClass, propertyName);
                    value = new Component(model);
                    BindComponent(subnode, (Component) value, subreflectedClass, className, subpath, isNullable);
                }
                else if ("parent".Equals(name))
                    model.ParentProperty = propertyName;

                if (value != null)
                    model.AddProperty(CreateProperty(value, propertyName, model.ComponentClass, subnode));
            }
        }
Exemplo n.º 25
0
		/// <summary>
		/// Called for Maps
		/// </summary>
		private void BindMapSecondPass(HbmMap mapMapping, Map model,
			IDictionary<string, PersistentClass> persistentClasses, IDictionary<string, MetaAttribute> inheritedMetas)
		{
			BindCollectionSecondPass(mapMapping, model, persistentClasses, inheritedMetas);

			HbmIndex indexMapping;
			HbmMapKey mapKeyMapping;

			HbmIndexManyToMany indexManyToManyMapping;
			HbmMapKeyManyToMany mapKeyManyToManyMapping;

			HbmCompositeIndex compositeIndexMapping;
			HbmCompositeMapKey compositeMapKeyMapping;

			HbmIndexManyToAny indexManyToAnyMapping;

			if ((indexMapping = mapMapping.Item as HbmIndex) != null)
			{
				var value = new SimpleValue(model.CollectionTable);
				new ValuePropertyBinder(value, Mappings).BindSimpleValue(indexMapping, IndexedCollection.DefaultIndexColumnName,
				                                                         model.IsOneToMany);
				model.Index = value;
				if (string.IsNullOrEmpty(model.Index.TypeName))
					throw new MappingException("map index element must specify a type: " + model.Role);
			}
			else if ((mapKeyMapping = mapMapping.Item as HbmMapKey) != null)
			{
				var value = new SimpleValue(model.CollectionTable);
				new ValuePropertyBinder(value, Mappings).BindSimpleValue(mapKeyMapping, IndexedCollection.DefaultIndexColumnName,
																																 model.IsOneToMany);
				model.Index = value;
				if (string.IsNullOrEmpty(model.Index.TypeName))
					throw new MappingException("map index element must specify a type: " + model.Role);
			}
			else if ((indexManyToManyMapping = mapMapping.Item as HbmIndexManyToMany) != null)
			{
				var manyToOne = new ManyToOne(model.CollectionTable);
				BindIndexManyToMany(indexManyToManyMapping, manyToOne, IndexedCollection.DefaultIndexColumnName, model.IsOneToMany);
				model.Index = manyToOne;
			}
			else if ((mapKeyManyToManyMapping = mapMapping.Item as HbmMapKeyManyToMany) != null)
			{
				var manyToOne = new ManyToOne(model.CollectionTable);
				BindMapKeyManyToMany(mapKeyManyToManyMapping, manyToOne, IndexedCollection.DefaultIndexColumnName, model.IsOneToMany);
				model.Index = manyToOne;
			}
			else if ((compositeIndexMapping = mapMapping.Item as HbmCompositeIndex) != null)
			{
				var component = new Component(model);
				BindComponent(compositeIndexMapping, component, null, null, model.Role + ".index", model.IsOneToMany, inheritedMetas);
				model.Index = component;
			}
			else if ((compositeMapKeyMapping = mapMapping.Item as HbmCompositeMapKey) != null)
			{
				var component = new Component(model);
				BindComponent(compositeMapKeyMapping, component, null, null, model.Role + ".index", model.IsOneToMany, inheritedMetas);
				model.Index = component;
			}
			else if ((indexManyToAnyMapping = mapMapping.Item as HbmIndexManyToAny) != null)
			{
				var any = new Any(model.CollectionTable);
				BindIndexManyToAny(indexManyToAnyMapping, any, model.IsOneToMany);
				model.Index = any;				
			}
		}
Exemplo n.º 26
0
		public Component(Component component)
			: base(component.Table)
		{
			owner = component.Owner;
		}
Exemplo n.º 27
0
		private void BindJoin(XmlNode node, Join join, IDictionary<string, MetaAttribute> inheritedMetas)
		{
			PersistentClass persistentClass = join.PersistentClass;
			String path = persistentClass.EntityName;

			// TABLENAME

			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;

			XmlAttribute actionNode = node.Attributes["schema-action"];
			string action = actionNode == null ? "all" : actionNode.Value;

			Table table = mappings.AddTable(schema, catalog, GetClassTableName(persistentClass, node), null, false, action);
			join.Table = table;

			XmlAttribute fetchNode = node.Attributes["fetch"];
			if (fetchNode != null)
				join.IsSequentialSelect = "select".Equals(fetchNode.Value);

			XmlAttribute invNode = node.Attributes["inverse"];
			if (invNode != null)
				join.IsInverse = "true".Equals(invNode.Value);

			XmlAttribute nullNode = node.Attributes["optional"];
			if (nullNode != null)
				join.IsOptional = "true".Equals(nullNode.Value);

			log.InfoFormat("Mapping class join: {0} -> {1}", persistentClass.EntityName, join.Table.Name);

			// KEY
			XmlNode keyNode = node.SelectSingleNode(HbmConstants.nsKey, namespaceManager);
			SimpleValue key = new DependantValue(table, persistentClass.Identifier);
			join.Key = key;
			if (keyNode.Attributes["on-delete"] != null)
				key.IsCascadeDeleteEnabled = "cascade".Equals(keyNode.Attributes["on-delete"].Value);
			BindSimpleValue(keyNode, key, false, persistentClass.EntityName);

			join.CreatePrimaryKey(dialect);
			join.CreateForeignKey();

			// PROPERTIES
			//PropertiesFromXML(node, persistentClass, mappings);
			foreach (XmlNode subnode in node.ChildNodes)
			{
				//I am only concerned with elements that are from the nhibernate namespace
				if (subnode.NamespaceURI != Configuration.MappingSchemaXMLNS)
					continue;

				string name = subnode.Name;
				XmlAttribute nameAttribute = subnode.Attributes["name"];
				string propertyName = nameAttribute == null ? null : nameAttribute.Value;
				IValue value = null;
				var collectionBinder = new CollectionBinder(this);
				if (collectionBinder.CanCreate(name))
				{
					Mapping.Collection collection = collectionBinder.Create(name, subnode, persistentClass.EntityName, propertyName,
					                                                        persistentClass, persistentClass.MappedClass,
					                                                        inheritedMetas);

					mappings.AddCollection(collection);
					value = collection;
				}
				else
				{
					switch (name)
					{
						case "many-to-one":
							value = new ManyToOne(table);
							BindManyToOne(subnode, (ManyToOne) value, propertyName, true);
							break;
						case "any":
							value = new Any(table);
							BindAny(subnode, (Any) value, true);
							break;
						case "property":
							value = new SimpleValue(table);
							BindSimpleValue(subnode, (SimpleValue) value, true, propertyName);
							break;
						case "component":
						case "dynamic-component":
							string subpath = StringHelper.Qualify(path, propertyName);
							value = new Component(join);
							BindComponent(subnode, (Component) value, join.PersistentClass.MappedClass, join.PersistentClass.ClassName,
							              propertyName, subpath, true, inheritedMetas);
							break;
					}
				}
				if (value != null)
				{
					var prop = CreateProperty(value, propertyName, persistentClass.MappedClass.AssemblyQualifiedName, subnode,
					                          inheritedMetas);
					prop.IsOptional = join.IsOptional;
					join.AddProperty(prop);
				}
			}

			// CUSTOM SQL
			HandleCustomSQL(node, join);
		}