NH 保存子集合时外键为空

发布于 2024-09-28 02:34:50 字数 2187 浏览 2 评论 0原文

我尝试将 NHibernate 映射到现有的数据库结构。不幸的是我无法改变现有的结构

首先我会给出一些背景知识,然后解释问题本身

关系分析非常简单: alt text

Log 是主要实体。他与Form有一对一的关系。 外键是FormID

买家&卖家是表单实体的集合。 应用程序应将表单实体与买家和卖家一起保存

问题是由Buyer & 组成的主键。卖方实体。 ForegineKey 的关键组合 - FormID 和内部排序 int 编号 - tdNum

这反映在以下表单 FNH 映射中

public class FormLogMap : ClassMap<FormLog>
{
    public FormLogMap()
    {
        Table("BillOfSaleLog");

        Id(x => x.FormId).Column("FormID").GeneratedBy.Native();
        ....
        ....
        References<Form>(x => x.Form, "FormID").LazyLoad().ReadOnly();
    }
}

public class FormMap : ClassMap<Form>
{
    public FormMap()
    {

    Table("BillOfSaleForm");

        Id(x => x.Id).Column("FormID").GeneratedBy.Foreign("Log");
        ...
        ...

        HasOne<FormLog>(x => x.Log).Cascade.All();

        HasMany(x => x.Buyers).KeyColumn("FormID").Inverse().Cascade.All();

        HasMany(x => x.Sellers).KeyColumn("FormID").Inverse().Cascade.All();
    }
}


public class BuyerMap : ClassMap<Buyer>
{
    public BuyerMap()
    {

        Table("BillOfSaleBuyer");

        CompositeId()
                    .KeyReference(x => x.Form, "FormID")
                    .KeyProperty(x => x.InnerOrderId, "tdNum1");

         ....
         ....
    }
}

Seller is exectly the same

当我尝试保存通过编辑操作实体 我正在使用 MVC 来获取用户数据并将其设为对象。 绑定工作正常,并使对象与集合一起生成。

但是当我保存实体时收到以下错误: alt text

我希望 NHibernate 能够足够智能地设置 Buyer &卖家外键。 但事实上它们仍然没有价值。

可以通过手动设置外键来解决问题 如以下代码:

        //i have to set the form proerty in the childs
        //without of the lines NH will try to save the childs it with FormID = null
        foreach (var buyer in form.Buyers) { buyer.Form = form; }
        foreach (var seller in form.Sellers) { seller.Form = form; }

但是我正在寻找一个优雅且正确的解决方案

感谢您的阅读

I try to map NHibernate to an existing database structure. Unfortunately I can not change the existing structure.

First I'll give some background and then explain the problem itself

Relational analysis is quite simple:
alt text

Log is the main Entity. he has one-to-one relationship with Form.
the foreign key is FormID.

Buyer & Seller are collection of the form entity.
The application should save the form entity together with the buyers and the sellers.

The problem is the primary key consisting of the Buyer & Seller entity.
The key composite from ForegineKey- FormID and an intenal ordering int number- tdNum

This is reflected in the following form FNH mapping

public class FormLogMap : ClassMap<FormLog>
{
    public FormLogMap()
    {
        Table("BillOfSaleLog");

        Id(x => x.FormId).Column("FormID").GeneratedBy.Native();
        ....
        ....
        References<Form>(x => x.Form, "FormID").LazyLoad().ReadOnly();
    }
}

public class FormMap : ClassMap<Form>
{
    public FormMap()
    {

    Table("BillOfSaleForm");

        Id(x => x.Id).Column("FormID").GeneratedBy.Foreign("Log");
        ...
        ...

        HasOne<FormLog>(x => x.Log).Cascade.All();

        HasMany(x => x.Buyers).KeyColumn("FormID").Inverse().Cascade.All();

        HasMany(x => x.Sellers).KeyColumn("FormID").Inverse().Cascade.All();
    }
}


public class BuyerMap : ClassMap<Buyer>
{
    public BuyerMap()
    {

        Table("BillOfSaleBuyer");

        CompositeId()
                    .KeyReference(x => x.Form, "FormID")
                    .KeyProperty(x => x.InnerOrderId, "tdNum1");

         ....
         ....
    }
}

Seller is exectly the same

The problem occurs when I try to save the entity by the Edit Action
I'm using MVC to get the user data and make it an object.
The binding works fine and makes the object along with the collections.

But when I save an entity receive the following error:
alt text

I expect NHibernate be smart enough to set the Buyer & Seller foreign keys.
But in fact they remain without value.

The problem can be solved by setting foreign keys manually
as the following code:

        //i have to set the form proerty in the childs
        //without of the lines NH will try to save the childs it with FormID = null
        foreach (var buyer in form.Buyers) { buyer.Form = form; }
        foreach (var seller in form.Sellers) { seller.Form = form; }

But I'm looking for an elegant and correct solution

Thank you for reading

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

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

发布评论

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

评论(2

我的鱼塘能养鲲 2024-10-05 02:34:50

您没有显示向表单添加买家或卖家的代码,但它应该如下所示:

form.Buyers.Add(buyer);
buyer.Form = form;

因为表单是关系的反面,所以您必须在多方设置对表单的引用。无论如何,您都应该这样做,以便内存中的对象是正确的。如果买方和卖方对象已经持久存在于同一个 ISession 中,那么应该可以工作。

You don't show the code for adding a Buyer or Seller to a Form but it should look like this:

form.Buyers.Add(buyer);
buyer.Form = form;

Because Form is the inverse side of the relationships, you have to set the reference to Form on the many side. You should do this anyway so that the in-memory objects are correct. If the Buyer and Seller objects are already persistent in the same ISession than that should work.

倾城花音 2024-10-05 02:34:50

根据this相关问题,HHibernate不支持使用复合键进行级联(就像在 BuyerMap 中一样)。其中一个答案确实包含黑客解决这个问题,但你最终会得到一个多余的列。

According to this related question, HHibernate does not support cascade working with a composite key (as you have in BuyerMap). One of the answers does contain a hack to workaround this, but you will end up with a redundant column.

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