private void BindKey(JoinedSubclass subclass, HbmKey keyMapping, Table mytable)
		{
			// TODO : property-ref ?? 
			SimpleValue key = new DependantValue(mytable, subclass.Identifier);
			subclass.Key = key;
			key.IsCascadeDeleteEnabled = keyMapping.ondelete == HbmOndelete.Cascade;
			key.ForeignKeyName = keyMapping.foreignkey;

			new ValuePropertyBinder(key, Mappings).BindSimpleValue(keyMapping, subclass.EntityName, false);
		}
		public void HandleJoinedSubclass(PersistentClass model, XmlNode subnode, IDictionary<string, MetaAttribute> inheritedMetas)
		{
			JoinedSubclass subclass = new JoinedSubclass(model);

			BindClass(subnode, null, subclass, inheritedMetas);
			inheritedMetas = GetMetas(subnode.SelectNodes(HbmConstants.nsMeta, namespaceManager), inheritedMetas, true); // get meta's from <joined-subclass>
			// joined subclass
			if (subclass.EntityPersisterClass == null)
				subclass.RootClazz.EntityPersisterClass = typeof(JoinedSubclassEntityPersister);

			//table + schema names
			XmlAttribute schemaNode = subnode.Attributes["schema"];
			string schema = schemaNode == null ? mappings.SchemaName : schemaNode.Value;
			XmlAttribute catalogNode = subnode.Attributes["catalog"];
			string catalog = catalogNode == null ? mappings.CatalogName : catalogNode.Value;

			XmlAttribute actionNode = subnode.Attributes["schema-action"];
			string action = actionNode == null ? "all" : actionNode.Value;
            
			Table mytable = mappings.AddTable(schema, catalog, GetClassTableName(subclass, subnode), null, false, action);
			((ITableOwner)subclass).Table = mytable;

			log.InfoFormat("Mapping joined-subclass: {0} -> {1}", subclass.EntityName, subclass.Table.Name);

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

			subclass.CreatePrimaryKey(dialect);

			if (!subclass.IsJoinedSubclass)
				throw new MappingException(
					"Cannot map joined-subclass " + subclass.EntityName + " to table " +
						subclass.Table.Name + ", the same table as its base class.");

			subclass.CreateForeignKey();

			// CHECK
			XmlAttribute chNode = subnode.Attributes["check"];
			if (chNode != null)
				mytable.AddCheckConstraint(chNode.Value);

			// properties
			PropertiesFromXML(subnode, subclass, inheritedMetas);

			model.AddSubclass(subclass);
			mappings.AddClass(subclass);
		}
		private void BindJoin(HbmJoin joinMapping, Join join, IDictionary<string, MetaAttribute> inheritedMetas)
		{
			PersistentClass persistentClass = join.PersistentClass;

			// TABLENAME
			string schema = joinMapping.schema ?? mappings.SchemaName;
			string catalog = joinMapping.catalog ?? mappings.CatalogName;

			string action = "all"; // joinMapping.schemaaction ?? "all";

			string tableName = joinMapping.table;
			Table table = mappings.AddTable(schema, catalog, GetClassTableName(persistentClass, tableName), joinMapping.Subselect, false, action);
			join.Table = table;

			join.IsSequentialSelect = joinMapping.fetch == HbmJoinFetch.Select;
			join.IsInverse = joinMapping.inverse;
			join.IsOptional = joinMapping.optional;

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

			// KEY
			SimpleValue key = new DependantValue(table, persistentClass.Identifier);
			key.ForeignKeyName = joinMapping.key.foreignkey;
			join.Key = key;
			key.IsCascadeDeleteEnabled = joinMapping.key.ondelete == HbmOndelete.Cascade;
			new ValuePropertyBinder(key, Mappings).BindSimpleValue(joinMapping.key, persistentClass.EntityName, false);

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

			// PROPERTIES
			new PropertiesBinder(Mappings, persistentClass, dialect).Bind(joinMapping.Properties, join.Table,
			                                                                                inheritedMetas, p => { },
			                                                                                join.AddProperty);

			// CUSTOM SQL
			HandleCustomSQL(joinMapping, join);
		}
		private void BindKey(HbmKey keyMapping, Mapping.Collection model)
		{
			if (keyMapping == null)
			{
				return;
			}
			string propRef = model.ReferencedPropertyName;
			IKeyValue keyValue;
			if (propRef == null)
			{
				keyValue = model.Owner.Identifier;
			}
			else
			{
				keyValue = (IKeyValue) model.Owner.GetProperty(propRef).Value;
			}
			var key = new DependantValue(model.CollectionTable, keyValue)
						{IsCascadeDeleteEnabled = keyMapping.ondelete == HbmOndelete.Cascade};

			new ValuePropertyBinder(key, Mappings).BindSimpleValue(keyMapping, Mapping.Collection.DefaultKeyColumnName, model.IsOneToMany);

			if (key.Type.ReturnedClass.IsArray)
				throw new MappingException("illegal use of an array as an identifier (arrays don't reimplement Equals)");
			model.Key = key;

			key.SetNullable(!keyMapping.IsNullable.HasValue || keyMapping.IsNullable.Value);
			key.SetUpdateable(!keyMapping.IsUpdatable.HasValue || keyMapping.IsUpdatable.Value);
			BindForeignKey(keyMapping.foreignkey, key);
		}
Пример #5
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);
        }
Пример #6
0
		private void BindJoin(HbmJoin joinMapping, Join join, IDictionary<string, MetaAttribute> inheritedMetas)
		{
			PersistentClass persistentClass = join.PersistentClass;

			// TABLENAME
			string schema = joinMapping.schema ?? mappings.SchemaName;
			string catalog = joinMapping.catalog ?? mappings.CatalogName;

			string action = "all"; // joinMapping.schemaaction ?? "all";

			string tableName = joinMapping.table;
			Table table = mappings.AddTable(schema, catalog, GetClassTableName(persistentClass, tableName), joinMapping.Subselect, false, action);
			join.Table = table;

			join.IsSequentialSelect = joinMapping.fetch == HbmJoinFetch.Select;
			join.IsInverse = joinMapping.inverse;
			join.IsOptional = joinMapping.optional;

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

			// KEY
			SimpleValue key;
			if (!String.IsNullOrEmpty(joinMapping.key.propertyref))
			{
				string propertyRef = joinMapping.key.propertyref;
				var propertyRefKey = new SimpleValue(persistentClass.Table)
					{
						IsAlternateUniqueKey = true
					};
				var property = persistentClass.GetProperty(propertyRef);
				join.RefIdProperty = property;
				//we only want one column
				var column = (Column) property.ColumnIterator.First();
				if (!column.Unique)
					throw new MappingException(
						string.Format(
							"Property {0}, on class {1} must be marked as unique to be joined to with a property-ref.",
							property.Name,
							persistentClass.ClassName));
				propertyRefKey.AddColumn(column);
				propertyRefKey.TypeName = property.Type.Name;
				key = new ReferenceDependantValue(table, propertyRefKey);
			}
			else
			{
				key = new DependantValue(table, persistentClass.Identifier);
			}

			key.ForeignKeyName = joinMapping.key.foreignkey;
			join.Key = key;
			key.IsCascadeDeleteEnabled = joinMapping.key.ondelete == HbmOndelete.Cascade;
			new ValuePropertyBinder(key, Mappings).BindSimpleValue(joinMapping.key, persistentClass.EntityName, false);

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

			// PROPERTIES
			new PropertiesBinder(Mappings, persistentClass, dialect).Bind(joinMapping.Properties, join.Table,
																							inheritedMetas, p => { },
																							join.AddProperty);

			// CUSTOM SQL
			HandleCustomSQL(joinMapping, join);
		}
Пример #7
0
		private void BindJoinToPersistentClass(Join join, Ejb3JoinColumn[] ejb3JoinColumns)
		{
			SimpleValue key = new DependantValue(join.Table, persistentClass.Identifier);
			join.Key = key;
			SetFKNameIfDefined(join);
			key.IsCascadeDeleteEnabled = false;
			TableBinder.BindFk(persistentClass, null, ejb3JoinColumns, key, false, mappings);
			join.CreatePrimaryKey(dialect);
			join.CreateForeignKey();
			persistentClass.AddJoin(join);
		}
Пример #8
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;
				}
			}
		}
Пример #9
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);
		}