NHibernate一对多重新检索对象问题

发布于 2024-10-05 18:25:11 字数 1671 浏览 0 评论 0原文

我遇到了一个关于复合键和一对多关系的奇怪问题。

我有以下发票类:

public class Invoice: AccountingBase<InvoiceItem>
{
    public virtual IList<InvoiceItem> InvoiceItems {.....}
}

它继承的类是(AccountingBase)如下:

public abstract class AccountingBase<TItemType>  : AccountingBase where TItemType : AccountingItemBase
{
    public virtual ObservableCollection<TItemType> Items { get; set; }

    public abstract void AddItem(TItemType item);
}

底层类是:

public abstract class AccountingBase
{
    public virtual Subsidiary Subsidiary {....}
    public virtual int ID{ ....}
}

发票类的映射如下:

 <class name="Invoice" table="Invoice">
     <composite-id>
       <key-many-to-one class="Subsidiary" name="Subsidiary"/>
       <key-property name="ID" column="InvoiceID" />
     </composite-id>
     <bag name="InvoiceItems" table="InvoiceItem" inverse="true" lazy="false">
       <key>
         <column name="Subsidiary" />
         <column name="InvoiceID" />
       </key>
       <one-to-many class="InvoiceItem" />
     </bag>   
 </class>

现在,当我检索发票列表时,它工作正常,包括延迟加载每个发票项目对象。我遇到的问题是,当我选择要编辑的发票时。我打开一个新会话,并尝试使用新会话检索相同的发票,但是当它加载发票时,它不会加载项目,而是将它们检索为空白,尽管当我手动运行生成的 SQL 时,它运行得很好。可能是什么原因?

我运行的代码是使用旧对象在新会话中检索以获取新发票对象:

Invoice = Session.Get<Invoice>(invoice);

当我调试时,发票的类型为发票,而且发票在 InvoiceItems 集合中有 1 个 InvoiceItem,但 Invoice 中的新实例,所有字段均已填充,但 InvoiceItems 为空。关于 Nhibernate 有什么我应该知道的可能导致这种错误的信息吗?

我尝试过lazy="false"

I have come across a weird problem concerning composite keys and one-to-many relationship.

I have the following Invoice class:

public class Invoice: AccountingBase<InvoiceItem>
{
    public virtual IList<InvoiceItem> InvoiceItems {.....}
}

the class it inherits is (AccountingBase) is as follows:

public abstract class AccountingBase<TItemType>  : AccountingBase where TItemType : AccountingItemBase
{
    public virtual ObservableCollection<TItemType> Items { get; set; }

    public abstract void AddItem(TItemType item);
}

And the bottom class is:

public abstract class AccountingBase
{
    public virtual Subsidiary Subsidiary {....}
    public virtual int ID{ ....}
}

The Mapping for Invoice class is as follows:

 <class name="Invoice" table="Invoice">
     <composite-id>
       <key-many-to-one class="Subsidiary" name="Subsidiary"/>
       <key-property name="ID" column="InvoiceID" />
     </composite-id>
     <bag name="InvoiceItems" table="InvoiceItem" inverse="true" lazy="false">
       <key>
         <column name="Subsidiary" />
         <column name="InvoiceID" />
       </key>
       <one-to-many class="InvoiceItem" />
     </bag>   
 </class>

Now when i retrieve a list of Invoices it works fine, including lazily loading each Items object. The problem i get is, when i select an invoice to edit. I open a new session, and i try retrieve the same invoice using the new session, but when it loads the invoice, it doesnt load the items, it retrieves them as blank, although when i run the generated SQL manually it runs perfectly fine. What could be the reason?

The code i am running to retrieve to get the new Invoice object in the new session using the old object is:

Invoice = Session.Get<Invoice>(invoice);

When i debug, invoice is of Type Invoice, and also, invoice has 1 InvoiceItem in InvoiceItems collection, but the new instance in Invoice, all the fields are populated but the InvoiceItems is null. Is there anything i should know about Nhibernate that might cause this kind of error?

I have tried lazy="false".

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

要走干脆点 2024-10-12 18:25:11

找到修复程序,我必须在发票类和子公司类中正确实现平等方法。

Found the fix, i had to correctly implement Equality methods, in both Invoice class and Subsidiary class.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文