简体   繁体   English

NHibernate子级未插入(非流利)

[英]NHibernate Child not inserting (not Fluent)

I am trying to insert the price attributes for a price. 我正在尝试为价格插入价格属性。 The price inserts, but the price attributes to don't. 价格插入,但价格属性不插入。 Price attribute has a foreign key of pat_prc_key that references the price key. Price属性具有一个引用价格键的外键pat_prc_key。 I have walked through a lot of the suggestions on Stack Overflow for setting up a child/parent relationship in Nhibernate, but I am stuck. 在Nhibernate中,我已经了解了关于Stack Overflow的许多建议,以建立子/父母关系,但是我被困住了。 Any suggestions would be greatly appreciated. 任何建议将不胜感激。 I included all of the code below. 我在下面包含了所有代码。

Price xml 价格xml

    <?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
  <class name="Api.Models.Price, Aafp.Also.Api" table="price" where="prc_delete_flag = 0">
    <id name="Key" column="prc_key" type="Guid" unsaved-value="00000000-0000-0000-0000-000000000000">
      <generator class="guid" />
    </id>
    <property name="AddUser" type="String" column="prc_add_user" />
    <property name="AddDate" type="DateTime" column="prc_add_date" />
    <property name="ChangeUser" type="String" column="prc_change_user" insert="false" />
    <property name="ChangeDate" type="DateTime" column="prc_change_date" insert="false" />
    <property name="DeleteFlag" type="Boolean" column="prc_delete_flag" />
    <property name="EntityKey" type="Guid" column="prc_entity_key" />
    <property name="ProductKey" type="Guid" column="prc_prd_key" />
    <property name="ProductTypeKey" type="Guid" column="prc_prd_ptp_key" />
    <property name="ProductCompanyKey" type="Guid" column="prc_prd_atc_key" />
    <property name="PriceCode" type="String" column="prc_code" />
    <property name="PriceAmount" type="Decimal" column="prc_price" />
    <property name="PricePercent" type="Decimal" column="prc_percent" />
    <property name="PriceDisplayName" type="String" column="prc_display_name" />
    <property name="PriceRevenueKey" type="Guid" column="prc_gla_revenue_key" />
    <property name="PriceStartDate" type="DateTime" column="prc_start_date" />
    <property name="PriceEndDate" type="DateTime" column="prc_end_date" />
    <property name="PriceEwebCode" type="String" column="prc_eweb_code" />
    <property name="RenewUnpaidOrdersFlag" type="Boolean" column="prc_renew_unpaid_orders_flag" />
    <property name="AllowUnpaidOrdersFlag" type="Boolean" column="prc_allow_unpaid_orders_flag" />

    <bag name="PriceAttributes" inverse="true" cascade="all-delete-orphan" lazy="true">
      <key column="pat_prc_key" />
      <one-to-many class="Api.Models.PriceAttribute, Api" />
    </bag>
  </class>
</hibernate-mapping>

Price Attribute xml 价格属性xml

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
  <class name="Api.Models.PriceAttribute, Api" table="price_attribute" mutable="false" where="pat_delete_flag = 0">
    <id name="Key" column="pat_key" type="Guid" unsaved-value="00000000-0000-0000-0000-000000000000">
      <generator class="guid" />
    </id>
    <property name="AddUser" type="String" column="pat_add_user" />
    <property name="AddDate" type="DateTime" column="pat_add_date" />
    <property name="ChangeUser" type="String" column="pat_change_user" />
    <property name="ChangeDate" type="DateTime" column="pat_change_date" />
    <property name="DeleteFlag" type="Boolean" column="pat_delete_flag" />
    <property name="EntityKey" type="Guid" column="pat_entity_key" />
    <property name="Code" type="String" column="pat_code" />

    <many-to-one name="Price" class="Api.Models.Price, Api" column="prc_key"  />
  </class>
</hibernate-mapping>

Price Model 价格模型

namespace Api.Models
{
    public class Price
    {
        public virtual Guid Key { get; set; }

        public virtual string AddUser { get; set; }

        public virtual DateTime AddDate { get; set; }

        public virtual string ChangeUser { get; set; }

        public virtual DateTime ChangeDate { get; set; }

        public virtual bool DeleteFlag { get; set; }

        public virtual Guid EntityKey { get; set; }

        public virtual Guid ProductKey { get; set; }

        public virtual Guid ProductTypeKey { get; set; }

        public virtual Guid ProductCompanyKey { get; set; }

        public virtual string PriceCode { get; set; }

        public virtual decimal PriceAmount { get; set; }

        public virtual decimal PricePercent { get; set; }

        public virtual string PriceDisplayName { get; set; }

        public virtual Guid PriceRevenueKey { get; set; }

        public virtual DateTime PriceStartDate { get; set; }

        public virtual DateTime PriceEndDate { get; set; }

        public virtual string PriceEwebCode { get; set; }

        public virtual bool RenewUnpaidOrdersFlag { get; set; }

        public virtual bool AllowUnpaidOrdersFlag { get; set; }

        public virtual IList<PriceAttribute> PriceAttributes { get; set; }

    }
}  

Price Attribute Model 价格属性模型

namespace Api.Models
{
    public class PriceAttribute
    {
        public virtual Guid Key { get; set; }

        public virtual string AddUser { get; set; }

        public virtual DateTime AddDate { get; set; }

        public virtual string ChangeUser { get; set; }

        public virtual DateTime? ChangeDate { get; set; }

        public virtual bool DeleteFlag { get; set; }

        public virtual Guid EntityKey { get; set; }

        public virtual string Code { get; set; }

        public virtual Price Price { get; set; }
    }
}

Task Code 任务代码

 var price = new Price
                {
                    AddDate = DateTime.Now,
                    AddUser = webLogin,
                    PriceCode = activity.ActivityNumber,
                    PriceAmount = 0.00M,
                    PricePercent = 100.0000M,
                    PriceStartDate = DateTime.Now,
                    PriceEndDate = activity.ActivityEndDate.AddDays(1),
                    PriceEwebCode = activity.ActivityNumber,
                    RenewUnpaidOrdersFlag = true,
                    AllowUnpaidOrdersFlag = true,
                    PriceAttributes = new List<PriceAttribute>()
                };

                var priceAttribute = new PriceAttribute
                {
                    AddDate = DateTime.Now,
                    AddUser = webLogin,
                    Code = activity.ActivityNumber
                };

                price.PriceAttributes.Add(priceAttribute);
                priceAttribute.Price = price;

Command.Store(price);

        public new void Store(Price price)
        {
            base.Store(price);
            Session.Flush();
        }

Relation Parent-Child is defined by one column. 关系父子关系由一列定义。 The parent reference in child table. 子表中的父引用。 So, both two mappings (parent's <bag> and child's <many-to-one> ) must use it.. ie have the same column name 因此,两个映射(父级的<bag>和子级的<many-to-one> )都必须使用它。即具有相同的列名

// parent as is (wrong mapping)
<bag name="PriceAttributes" inverse="true" cascade="all-delete-orphan" lazy="true">
  <key column="pat_prc_key" /> // WRONG HERE

and

// child
<many-to-one name="Price" class="Api.Models.Price, Api" 
    column="prc_key"  /> // child reference to parent

Other words, parent must be fixed to 换句话说,父项必须固定为

// parent as should be
  <key column="prc_key" /> // child reference to parent

Would suggest this: 建议如下:

After lots of trial and error and using the solution above the mapping wouldn't work. 经过大量的反复试验并使用上面的解决方案,映射将无法正常工作。 Hopefully this will help someone in the future. 希望这会在将来对某人有所帮助。 How they structured the database made it necessary to have two separate insert statements. 他们如何构造数据库使得必须具有两个单独的插入语句。

我建议使用ISet而不是IList,并将集合映射为Set而不是Bag。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM