当前位置: 动力学知识库 > 问答 > 编程问答 >

NHibernate collection mapping problem

问题描述:

I'm trying to map a relatively simple parent-children (Invoice-InvoiceEntry) scenario in NHibernate. Here are some parts of my mapping files:

from Invoice.hbm.xml

<set name="InvoiceEntries" table="InvoiceEntries" inverse="true" cascade="all-delete-orphan" lazy="false">

<key column="InvoiceID" />

<one-to-many class="Jobflow.Models.Entities.InvoiceEntry, Jobflow" />

</set>

from InvoiceEntry.hbm.xml

<many-to-one name="Invoice" class="Jobflow.Models.Entities.Invoice, Jobflow" column="InvoiceID" not-null="true" lazy="false" />

from Invoice.cs

private ISet<IInvoiceEntry> _invoiceEntries = new HashedSet<IInvoiceEntry>();

public virtual ISet<IInvoiceEntry> InvoiceEntries

{

get { return _invoiceEntries; }

set { _invoiceEntries = value;}

}

What happens is that when I save an invoice everything is fine. A new record is inserted into both tables. The value in column InvoiceID in the InvoiceEntries table is the same as the ID column in the Invoices table. However, when I try to load the invoice from the repository via NHibernate I get the following error:

NHibernate.ObjectNotFoundException: No row with the given identifier exists[Jobflow.Models.Entities.InvoiceEntry#55]

55 is indeed the correct ID in the Invoices table and the correct InvoiceID in the InvoiceEntries table. However, it seems like NHibernate might be looking for the InvoiceEntries PK which is EnvoiceEntryID instead.

Can anybody help me with this?

网友答案:

Well this one is solved and it's my stupidity again to blame. It turns out that the InvoiceEntry entity collection was incorrectly linked from another entity as well. If anybody is interested in the details let me know.

分享给朋友:
您可能感兴趣的文章:
随机阅读: