Nhibernate映射一个具有泛型list属性的类
本文关键字:泛型 list 属性 一个 映射 Nhibernate | 更新日期: 2023-09-27 18:05:53
我是Nhibernate的新手,我正试图映射一个类(发票),它具有使用Nhibernate的通用列表属性,但我不断得到错误:"从表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>
看起来不错!
修复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" />
修复包同样,一个包不能是多对一的——它应该是一对多的。请参阅文档。取代…
<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了解正确实现此功能的各种方法。