Fluent NHibernate - 将 2 个表映射到一个类

发布于 2024-08-16 17:17:09 字数 851 浏览 3 评论 0原文

我有两个表,我想将它们映射到一个类,如下所示:

CUSTOMER_INFO_CLASS.cs
----------------------
Id (CUSTOMER table) 
CustomerName (CUSTOMER table)
CustomerTypeDesc (CUSTOMER_TYPE table)

我尝试使用联接来完成此操作,如下所示:

Table("CUSTOMER");

Id(x => x.ID).Length(10).Column("CustomerId");
Map(x => x.CustomerName);

Join("CUSTOMER_TYPE", m =>
    {
    m.Optional();
    m.Map(x => x.CustomerTypeDesc);
    m.KeyColumn("CustomerType");
    });

问题是我尝试链接两个表的字段不是主键在其中任何一个。 (默认情况下,连接由定义为 ID 的字段完成) 所以我发现对于CUSTOMER_TYPE表我可以通过“KeyColumn”定义字段。
如何定义 CUSTOMER 表中的相关列是 CustomerTypeCode 而不是 CustomerId? (如果可以的话)

最后 sql 查询应该如下所示:

Select Id, CustomerName, CustomerAddress, CustomerTypeDesc
From CUSTOMER t1
  Left join CUSTOMER_TYPE t2
    On t1.CustomerTypeCode = t2.CustomerType

I have two tables that I want to map to one class that will looks like:

CUSTOMER_INFO_CLASS.cs
----------------------
Id (CUSTOMER table) 
CustomerName (CUSTOMER table)
CustomerTypeDesc (CUSTOMER_TYPE table)

I tried to do it with join, as follows:

Table("CUSTOMER");

Id(x => x.ID).Length(10).Column("CustomerId");
Map(x => x.CustomerName);

Join("CUSTOMER_TYPE", m =>
    {
    m.Optional();
    m.Map(x => x.CustomerTypeDesc);
    m.KeyColumn("CustomerType");
    });

The problem is that the field with whom I'm trying to link the two tables is not a primary key in any of them. (And by default the join done by the field that defined as ID)
So I found that for the CUSTOMER_TYPE table I can define the field by “KeyColumn”.
How can I define that the related column in the CUSTOMER table will be CustomerTypeCode and not CustomerId? (if I can at all)

At the end the sql query should looks like:

Select Id, CustomerName, CustomerAddress, CustomerTypeDesc
From CUSTOMER t1
  Left join CUSTOMER_TYPE t2
    On t1.CustomerTypeCode = t2.CustomerType

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

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

发布评论

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

评论(1

复古式 2024-08-23 17:17:09

如果 Customer 表将 CustomerType 成员映射到 CustomerType 表的主键,那么 Hibernate 应该自动为您执行连接。

CustomerType 未通过正常外键引用链接是否有原因?

If the Customer table maps the CustomerType member to the primary key of the CustomerType table, then Hibernate should do the join automatically for you.

Is there a reason why the CustomerType is not linked by a normal foreign key reference?

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