简体   繁体   English

Nhibernate映射具有泛型列表属性的类

[英]Nhibernate mapping a class which has a generic list property

I am new to Nhibernate and I am trying to map a class (Invoice) which has a generic list property using Nhibernate but I keep getting error: "An association from the table InvoiceDetails refers to an unmapped class: System.Int32". 我是Nhibernate的新手,我正在尝试使用Nhibernate映射具有通用列表属性的类(发票),但我不断收到错误消息:“表InvoiceDetails中的关联引用了未映射的类:System.Int32”。 Because each invoice has more details I have tought of one to many relation between Invoices and InvoiceDetails table. 因为每个发票都有更多详细信息,所以我在发票和InvoiceDetails表之间具有一对多的联系。

Details: a) Invoice class: 详细信息: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 class: 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) Invoice mapping file: 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 mapping file: 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>

Looks pretty good! 看起来还不错!

Fix InvoiceID 修正InvoiceID

You need to fix the InvoiceDetail.InvoiceID property. 您需要修复InvoiceDetail.InvoiceID属性。 With many-to-one properties, you should model them as references to the parent entity instead of just as simple value properties containing an ID. 对于多对一属性,您应该将它们建模为对父实体的引用,而不是像包含ID的简单值属性一样。 You got it right with Invoice.Institution . 您使用Invoice.Institution正确了。

In InvoiceDetail , replace this... InvoiceDetail ,替换此...

public virtual int InvoiceID { get; set; }

... with this: ... 有了这个:

public virtual Invoice Invoice { get; set; }

Likewise, in your mappings, replace this... 同样,在您的映射中,替换此...

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

... with this: ... 有了这个:

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

Fix Bag 固定袋

Also, a bag cannot be many-to-one - it should be one-to-many. 另外,袋子不能是多对一的,应该是一对多的。 See the Documentation . 请参阅文档 Replace this... 取代这个...

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

... with this: ... 有了这个:

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

Caution about Lazy=False 关于Lazy = False的警告

One final note: be careful with lazy="false". 最后一点:请注意lazy =“ false”。 If you are intending to improve performance be having NHibernate eagerly fetch those related records, this is not the way to do it. 如果您打算提高性能,那就让NHibernate急切地获取那些相关记录,这不是做到这一点的方法。 See http://ayende.com/blog/4573/nhibernate-is-lazy-just-live-with-it for an explanation of why this is a problem. 有关为什么会出现此问题的说明,请参见http://ayende.com/blog/4573/nhibernate-is-lazy-just-live-with-it Refer to http://nhibernate.info/doc/nh/en/index.html#performance-fetching for the various way to implement this correctly. 请参阅http://nhibernate.info/doc/nh/en/index.html#performance-fetching,以了解各种正确实现此方法的方法。

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

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