Fluent NHibernate - 将 2 个表映射到一个类
我有两个表,我想将它们映射到一个类,如下所示:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果 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?