0

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. 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. Any suggestions would be greatly appreciated. I included all of the code below.

Price 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 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();
        }
Matituk
  • 27
  • 9

3 Answers3

0

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.. i.e. have the same column name

// 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:

Radim Köhler
  • 122,561
  • 47
  • 239
  • 335
  • I guess the problem must lie somewhere else, since configuring wrong column(names) in the mapping should result in a SQL statement that could not be executed; hence you should see an exception in that case. – Frederik Gheysels Feb 13 '18 at 19:30
0

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.

Matituk
  • 27
  • 9
0

I would suggest using an ISet instead of a IList and map the collection as a Set instead of a Bag.

Frederik Gheysels
  • 56,135
  • 11
  • 101
  • 154