Nhibernate映射具有通用列表属性的类

时间:2013-03-26 16:57:14

标签: c# nhibernate nhibernate-mapping generic-list

我是Nhibernate的新手,我正在尝试使用Nhibernate映射一个具有通用列表属性的类(Invoice),但我一直在 错误:“表InvoiceDetails中的关联引用了未映射的类:System.Int32”。 因为每张发票都有更多详细信息,我在发票和InvoiceDetails表之间建立了一对多关系。

详细说明: a)发票类:

public class Invoice
{
    private IList<InvoiceDetail> _invoiceDetails = new List<InvoiceDetail>();

    public virtual int InvoiceID { get; set; }
    public virtual string SerialNumber { get; set; }
    public virtual string Number { get; set; }
    public virtual DateTime InvoiceDate { get; set; }
    public virtual decimal Amount { get; set; }
    public virtual IList<InvoiceDetail> InvoiceDetails
    {
        get { return _invoiceDetails; }
        set { _invoiceDetails = value; }
    }
    public virtual Customer customer { get; set; }
    public virtual Institution Institution { get; set; }
    public virtual Receipt receipt { get; set; }
    public virtual bool IsDeleted { get; set; }
}

b)InvoiceDetail类:

public class InvoiceDetail
{
    public virtual int InvoiceDetailID { get; set; }
    public virtual int InvoiceID { get; set; }
    public virtual string ServiceDescription { get; set; }
    public virtual string Unit { get; set; }
    public virtual int Quantity { get; set; }
    public virtual double Value { get; set; }
    public virtual bool IsDeleted { get; set; }
}

c)发票映射文件:

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
    assembly="MyProject" namespace="MyProject.Model">
    <class name="Invoice" table="Invoices">
        <id name="InvoiceID" column="InvoiceID" type="int">
            <generator class="native"></generator>
        </id>
        <property name="SerialNumber" column ="InvoiceSerialNumber"/>
        <property name="Number" column ="InvoiceNumber"/>
        <property name="InvoiceDate"/>
        <property name="Amount"/>
        <property name="IsDeleted" />
        <many-to-one name="Institution" column="InstitutionID" />
        <bag name="InvoiceDetails" access="nosetter.camelcase"
            inverse ="true" lazy ="false" cascade="all-delete-orphan">
            <key column="InvoiceID"/>
            <many-to-one class="MyProject.Model.InvoiceDetail, MyProject"/>
        </bag>
    </class>
</hibernate-mapping>

d)InvoiceDetail映射文件:

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
    assembly="MyProject" namespace="MyProject.Model">
    <class name="InvoiceDetail" table="InvoiceDetails">
        <id name="InvoiceDetailID" column="InvoiceDetailID" type="int">
            <generator class="native"></generator>
         </id>
         <many-to-one name="InvoiceID" column="InvoiceID" />
         <property name="ServiceDescription"/>
         <property name="Unit"/>
         <property name="Quantity"/>
         <property name="Value"/>
         <property name="IsDeleted" />    
     </class>
 </hibernate-mapping>

1 个答案:

答案 0 :(得分:0)

看起来很不错!

修复InvoiceID

您需要修复InvoiceDetail.InvoiceID属性。对于多对一属性,您应该将它们建模为对父实体的引用,而不是仅包含ID的简单值属性。你使用Invoice.Institution得到了正确的结果。

InvoiceDetail中,替换此内容......

public virtual int InvoiceID { get; set; }

......用这个:

public virtual Invoice Invoice { get; set; }

同样,在你的映射中,替换它......

<many-to-one name="InvoiceID" column="InvoiceID" />

......用这个:

<many-to-one name="Invoice" column="InvoiceID" />

修理袋

另外,一个包不能多对一 - 它应该是一对多的。请参阅Documentation。替换这个......

<bag name="InvoiceDetails" access="nosetter.camelcase" inverse ="true" lazy ="false" cascade="all-delete-orphan">
  <key column="InvoiceID"/>
  <many-to-one class="MyProject.Model.InvoiceDetail, MyProject"/>
</bag>

......用这个:

<bag name="InvoiceDetails" access="nosetter.camelcase" inverse ="true" lazy ="false" cascade="all-delete-orphan">
  <key column="InvoiceID"/>
  <one-to-many class="MyProject.Model.InvoiceDetail, MyProject"/>
</bag>

关于Lazy = False

的注意事项

最后一点:小心lazy =“false”。如果您打算提高性能,让NHibernate急切地获取那些相关记录,这不是实现它的方法。有关此问题的原因,请参阅http://ayende.com/blog/4573/nhibernate-is-lazy-just-live-with-it。有关正确实现此方法的各种方法,请参阅http://nhibernate.info/doc/nh/en/index.html#performance-fetching