/// <summary>
        /// Visits the one to one.
        /// </summary>
        /// <remarks>
        /// Infer the type on the other side
        /// </remarks>
        /// <param name="model">The model.</param>
        public override void VisitOneToOne(OneToOneModel model)
        {
            if (model.OneToOneAtt.MapType == null)
            {
                model.OneToOneAtt.MapType = model.Property.PropertyType;
            }

            base.VisitOneToOne(model);
        }
Exemplo n.º 2
0
        public override void VisitOneToOne(OneToOneModel model)
        {
            String cascade = TranslateCascadeEnum(model.OneToOneAtt.Cascade);

            AppendF("<one-to-one {0} {1} {2} {3} {4} {5}/>",
                    MakeAtt("name", model.Property.Name),
                    MakeAtt("access", model.OneToOneAtt.AccessString),
                    MakeAtt("class", MakeTypeName(model.Property.PropertyType)),
                    WriteIfNonNull("cascade", cascade),
                    WriteIfNonNull("outer-join", TranslateOuterJoin(model.OneToOneAtt.OuterJoin)),
                    WriteIfTrue("constrained", model.OneToOneAtt.Constrained));
        }
        /// <summary>
        /// Visits the primary key.
        /// </summary>
        /// <remarks>
        /// Infer column name and the reverse property if using [OneToOne]
        /// </remarks>
        /// <param name="model">The model.</param>
        public override void VisitPrimaryKey(PrimaryKeyModel model)
        {
            if (model.PrimaryKeyAtt.Column == null)
            {
                model.PrimaryKeyAtt.Column = model.Property.Name;
            }

            // Append column prefix
            model.PrimaryKeyAtt.Column = columnPrefix + model.PrimaryKeyAtt.Column;

            if (model.PrimaryKeyAtt.Generator == PrimaryKeyType.Foreign)
            {
                // Let's see if we are an OneToOne

                if (currentModel.OneToOnes.Count != 0)
                {
                    // Yes, set the one to one as param

                    OneToOneModel oneToOne = currentModel.OneToOnes[0];

                    String param = "property=" + oneToOne.Property.Name;

                    if (model.PrimaryKeyAtt.Params == null)
                    {
                        model.PrimaryKeyAtt.Params = param;
                    }
                    else
                    {
                        model.PrimaryKeyAtt.Params += "," + param;
                    }
                }
            }
            else if (model.PrimaryKeyAtt.Generator == PrimaryKeyType.Custom)
            {
                if (model.PrimaryKeyAtt.CustomGenerator == null)
                {
                    throw new ActiveRecordException(String.Format(
                                                        "A type defined that its primary key would use a custom generator, " +
                                                        "but apparently forgot to define the custom generator using PrimaryKeyAttribute.CustomGenerator property. " +
                                                        "Check type {0}", currentModel.Type.FullName));
                }

                if (!typeof(IIdentifierGenerator).IsAssignableFrom(model.PrimaryKeyAtt.CustomGenerator))
                {
                    throw new ActiveRecordException(
                              "The custom generator associated with the PK for the type " + currentModel.Type.FullName +
                              "does not implement interface NHibernate.Id.IIdentifierGenerator");
                }
            }
        }
		/// <summary>
		/// Visits the one to one.
		/// </summary>
		/// <remarks>
		/// Infer the type on the other side
		/// </remarks>
		/// <param name="model">The model.</param>
		public override void VisitOneToOne(OneToOneModel model)
		{
			if (model.OneToOneAtt.MapType == null)
			{
				model.OneToOneAtt.MapType = model.Property.PropertyType;
			}

			base.VisitOneToOne(model);
		}
Exemplo n.º 5
0
		/// <summary>
		/// Visits the one to one.
		/// </summary>
		/// <param name="model">The model.</param>
		public override void VisitOneToOne(OneToOneModel model)
		{
			String cascade = TranslateCascadeEnum(model.OneToOneAtt.Cascade);

			AppendF("<one-to-one{0}{1}{2}{3}{4}{5}{6}{7} />",
			        MakeAtt("name", model.Property.Name),
			        MakeAtt("access", model.OneToOneAtt.AccessString),
			        MakeAtt("class", MakeTypeName(model.OneToOneAtt.MapType)),
			        WriteIfNonNull("cascade", cascade),
			        WriteIfNonNull("property-ref", model.OneToOneAtt.PropertyRef),
					WriteIfNonNull("foreign-key", model.OneToOneAtt.ForeignKey),
					WriteIfNonNull("fetch", TranslateFetch(model.OneToOneAtt.Fetch)),
					WriteIfTrue("constrained", model.OneToOneAtt.Constrained));
		}
Exemplo n.º 6
0
 public virtual void VisitOneToOne(OneToOneModel model)
 {
 }
 /// <summary>
 /// Visits the one to one.
 /// </summary>
 /// <param name="model">The model.</param>
 public virtual void VisitOneToOne(OneToOneModel model)
 {
 }
        public override void VisitPrimaryKey(PrimaryKeyModel model)
        {
            // check for composite key first
            if (model.Property.PropertyType.GetCustomAttributes(typeof(CompositeKeyAttribute), false).Length > 0)
            {
                MethodInfo eq = model.Property.PropertyType.GetMethod("Equals", BindingFlags.Instance | BindingFlags.Public | BindingFlags.DeclaredOnly);
                MethodInfo hc = model.Property.PropertyType.GetMethod("GetHashCode", BindingFlags.Instance | BindingFlags.Public | BindingFlags.DeclaredOnly);

                if (eq == null || hc == null)
                {
                    throw new ActiveRecordException(string.Format("To use type '{0}' as a composite id, you must implement Equals and GetHashCode.", model.Property.PropertyType.Name));
                }

                if (model.Property.PropertyType.IsSerializable == false)
                {
                    throw new ActiveRecordException(string.Format("To use type '{0}' as a composite id it must be Serializable.", model.Property.PropertyType.Name));
                }

                int            keyPropAttrCount  = 0;
                PropertyInfo[] compositeKeyProps = model.Property.PropertyType.GetProperties();
                foreach (PropertyInfo keyProp in compositeKeyProps)
                {
                    if (keyProp.GetCustomAttributes(typeof(KeyPropertyAttribute), false).Length > 0)
                    {
                        keyPropAttrCount++;
                    }
                }
                if (keyPropAttrCount < 2)
                {
                    throw new ActiveRecordException(string.Format("To use type '{0}' as a composite id it must have two or more properties marked with the [KeyProperty] attribute.", model.Property.PropertyType.Name));
                }
            }
            else
            {
                if (model.PrimaryKeyAtt.Column == null)
                {
                    model.PrimaryKeyAtt.Column = model.Property.Name;
                }

                if (model.PrimaryKeyAtt.Generator == PrimaryKeyType.Foreign)
                {
                    // Just a thought, let's see if we are a OneToOne

                    if (currentModel.OneToOnes.Count != 0)
                    {
                        // Yes, set the one to one as param

                        OneToOneModel oneToOne = (OneToOneModel)currentModel.OneToOnes[0];

                        String param = "property=" + oneToOne.Property.Name;

                        if (model.PrimaryKeyAtt.Params == null)
                        {
                            model.PrimaryKeyAtt.Params = param;
                        }
                        else
                        {
                            model.PrimaryKeyAtt.Params += "," + param;
                        }
                    }
                }
            }
        }