我正在尝试为价格插入价格属性。价格插入,但价格属性不要。 Price属性具有引用价格键的pat_prc_key的外键。我已经在Stack Overflow上提出了很多关于在Nhibernate中建立子/父关系的建议,但是我被困住了。任何建议将不胜感激。我在下面提供了所有代码。
价格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>
价格属性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>
价格模型
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; }
}
}
价格属性模型
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; }
}
}
任务代码
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();
}
答案 0 :(得分:0)
Relation Parent-Child由一列定义。子表中的父引用。因此,两个映射(父级&#39; s <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
和
// child
<many-to-one name="Price" class="Api.Models.Price, Api"
column="prc_key" /> // child reference to parent
换句话说,父母必须固定为
// parent as should be
<key column="prc_key" /> // child reference to parent
会建议:
答案 1 :(得分:0)
经过大量试验和错误并使用上述解决方案后,映射将无法正常工作。希望这将有助于未来的人。他们如何构建数据库使得必须有两个单独的插入语句。
答案 2 :(得分:0)
我建议使用ISet而不是IList,并将集合映射为Set而不是Bag。