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); }
/// <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; } } }