Exemplo n.º 1
0
        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 : true);
            key.SetUpdateable(keyMapping.IsUpdatable.HasValue ? keyMapping.IsUpdatable.Value : true);
            BindForeignKey(keyMapping.foreignkey, key);
        }
Exemplo n.º 2
0
        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);
        }
Exemplo n.º 3
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);
        }
Exemplo n.º 4
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 = 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);
        }
Exemplo n.º 5
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.Info("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();
            join.CreateForeignKey();

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

            // CUSTOM SQL
            HandleCustomSQL(joinMapping, join);
        }
Exemplo n.º 6
0
        /// <remarks>
        /// Called for all collections
        /// </remarks>
        private void BindCollectionSecondPass(XmlNode node, Mapping.Collection model,
                                              IDictionary <string, PersistentClass> persistentClasses, IDictionary <string, MetaAttribute> inheritedMetas)
        {
            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))
                {
                    var elt = new SimpleValue(model.CollectionTable);
                    model.Element = elt;
                    if (model.IsGeneric)
                    {
                        switch (model.GenericArguments.Length)
                        {
                        case 1:
                            // a collection with a generic type parameter
                            elt.TypeName = model.GenericArguments[0].AssemblyQualifiedName;
                            break;

                        case 2:
                            // a map (IDictionary) with 2 parameters
                            elt.TypeName = model.GenericArguments[1].AssemblyQualifiedName;
                            break;
                        }
                    }
                    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, null, null, model.Role + ".element", true, inheritedMetas);
                }
                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.º 7
0
        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);
        }