예제 #1
0
        public void Bind(HbmClass classSchema, IDictionary<string, MetaAttribute> inheritedMetas)
        {
            var rootClass = new RootClass();
            BindClass(classSchema, rootClass, inheritedMetas);
            // OPTIMISTIC LOCK MODE
            rootClass.OptimisticLockMode = classSchema.optimisticlock.ToOptimisticLock();

            inheritedMetas = GetMetas(classSchema, inheritedMetas, true); // get meta's from <class>

            //TABLENAME
            string schema = classSchema.schema ?? mappings.SchemaName;
            string catalog = classSchema.catalog ?? mappings.CatalogName;
            string tableName = GetClassTableName(rootClass, classSchema);
            if (string.IsNullOrEmpty(tableName))
            {
                throw new MappingException(
                    string.Format(
                        "Could not determine the name of the table for entity '{0}'; remove the 'table' attribute or assign a value to it.",
                        rootClass.EntityName));
            }

            Table table = mappings.AddTable(schema, catalog, tableName, classSchema.Subselect, rootClass.IsAbstract.GetValueOrDefault(), classSchema.schemaaction);
            ((ITableOwner) rootClass).Table = table;

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

            rootClass.IsMutable = classSchema.mutable;
            rootClass.Where = classSchema.where ?? rootClass.Where;

            if (classSchema.check != null)
                table.AddCheckConstraint(classSchema.check);

            rootClass.IsExplicitPolymorphism = classSchema.polymorphism == HbmPolymorphismType.Explicit;

            BindCache(classSchema.cache, rootClass);
            new ClassIdBinder(this).BindId(classSchema.Id, rootClass, table);
            new ClassCompositeIdBinder(this).BindCompositeId(classSchema.CompositeId, rootClass);
            new ClassDiscriminatorBinder(rootClass, Mappings).BindDiscriminator(classSchema.discriminator, table);
            BindTimestamp(classSchema.Timestamp, rootClass, table, inheritedMetas);
            BindVersion(classSchema.Version, rootClass, table, inheritedMetas);

            if (!String.IsNullOrEmpty(classSchema.primaryKeyName)) {
                rootClass.PrimaryKeyName = classSchema.primaryKeyName;
                table.PrimaryKeyName = classSchema.primaryKeyName;
            }
            rootClass.CreatePrimaryKey(dialect);
            BindNaturalId(classSchema.naturalid, rootClass, inheritedMetas);
            new PropertiesBinder(mappings, rootClass, dialect).Bind(classSchema.Properties, inheritedMetas);

            BindJoins(classSchema.Joins, rootClass, inheritedMetas);
            BindSubclasses(classSchema.Subclasses, rootClass, inheritedMetas);
            BindJoinedSubclasses(classSchema.JoinedSubclasses, rootClass, inheritedMetas);
            BindUnionSubclasses(classSchema.UnionSubclasses, rootClass, inheritedMetas);

            new FiltersBinder(rootClass, Mappings).Bind(classSchema.filter);

            mappings.AddClass(rootClass);
        }
		public void Bind(XmlNode node, HbmClass classSchema, IDictionary<string, MetaAttribute> inheritedMetas)
		{
			RootClass rootClass = new RootClass();
			BindClass(node, classSchema, rootClass, inheritedMetas);
			inheritedMetas = GetMetas(classSchema, inheritedMetas, true); // get meta's from <class>

			//TABLENAME
			string schema = classSchema.schema ?? mappings.SchemaName;
			string catalog = classSchema.catalog ?? mappings.CatalogName;
			string tableName = GetClassTableName(rootClass, classSchema);
			if (string.IsNullOrEmpty(tableName))
			{
				throw new MappingException(
					string.Format(
						"Could not determine the name of the table for entity '{0}'; remove the 'table' attribute or assign a value to it.",
						rootClass.EntityName));
			}

			Table table = mappings.AddTable(schema, catalog, tableName, null, rootClass.IsAbstract.GetValueOrDefault(), classSchema.schemaaction);
			((ITableOwner) rootClass).Table = table;

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

			rootClass.IsMutable = classSchema.mutable;
			rootClass.Where = classSchema.where ?? rootClass.Where;

			if (classSchema.check != null)
				table.AddCheckConstraint(classSchema.check);

			rootClass.IsExplicitPolymorphism = classSchema.polymorphism == HbmPolymorphismType.Explicit;

			BindCache(classSchema.cache, rootClass);
			new ClassIdBinder(this).BindId(classSchema.Id, rootClass, table);
			new ClassCompositeIdBinder(this).BindCompositeId(classSchema.CompositeId, rootClass);
			new ClassDiscriminatorBinder(this).BindDiscriminator(classSchema.discriminator, rootClass, table);
			BindTimestamp(classSchema.Timestamp, rootClass, table, inheritedMetas);
			BindVersion(classSchema.Version, rootClass, table, inheritedMetas);

			rootClass.CreatePrimaryKey(dialect);

			PropertiesFromXML(node, rootClass, inheritedMetas);
			mappings.AddClass(rootClass);
		}
        public virtual void Bind(ClassMapping classMapping)
        {
            RootClass rootClass = new RootClass();
            BindClass(classMapping, rootClass);
            string schema = mappings.SchemaName;
            string catalog = mappings.CatalogName;
            string tableName = classMapping.Tablename.ValueOrDefault(
                mappings.NamingStrategy.TableName(classMapping.Type.Name));

            Table table = mappings
                .AddTable(schema, catalog, tableName, null,
                          rootClass.IsAbstract.GetValueOrDefault());//Introduce SchemaAction
            ((ITableOwner)rootClass).Table = table;

            rootClass.IsMutable = true;
            rootClass.IsExplicitPolymorphism = false;
            new ClassIdBinder(this).BindId(classMapping.Id as IdMapping, rootClass, table);
            rootClass.CreatePrimaryKey(dialect);
            base.BindProperties(rootClass, classMapping);
            mappings.AddClass(rootClass);
        }
예제 #4
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 );
		}