流畅的 nHibernate 连接
我有一个映射到名为“规则”的表的实体。该实体的表与另一个称为“类别”的表具有 FK。我试图弄清楚如何从我的规则实体中的类别中提取属性。我很确定我想在实体映射中使用联接,但我不知道如何配置它以使其正常工作。这是我的映射:
Join("Category", x =>
{
x.Map(i => i.CategoryName, "Name");
x.KeyColumn("CategoryId");
x.Inverse();
});
这是它生成的 SQL...
SELECT ...
FROM Rule rules0_ left outer join Category rules0_1_ on rules0_.Id=rules0_1_.CategoryId
WHERE ...
这是我想要的 SQL。
SELECT ...
FROM Rule rules0_ left outer join Category rules0_1_ on rules0_.CategoryId=rules0_1_.Id
WHERE ...
我似乎在 JoinPart 上找不到任何可以让我执行此操作的内容。从我找到的一点文档来看,Subselect 看起来很有希望,但我找不到任何如何使用它的示例。任何有关这个问题的建议将不胜感激。谢谢!
I have an entity that maps to a table called Rule. The table for this entity has an FK to another Table called Category. I'm trying to figure out how to pull in a property from Category in my Rule entity. I'm pretty sure I want to use a join in my entity mapping, but I can't figure out how to configure it so that it works. Here is my mapping:
Join("Category", x =>
{
x.Map(i => i.CategoryName, "Name");
x.KeyColumn("CategoryId");
x.Inverse();
});
Here is the SQL that it's generating...
SELECT ...
FROM Rule rules0_ left outer join Category rules0_1_ on rules0_.Id=rules0_1_.CategoryId
WHERE ...
Here is the SQL that I want.
SELECT ...
FROM Rule rules0_ left outer join Category rules0_1_ on rules0_.CategoryId=rules0_1_.Id
WHERE ...
I can't seem to find anything on the JoinPart that will let me do this. Subselect looks promising from the little bit of documentation I've found, but I can't find any examples of how to use it. Any advice on this problem would be much appreciated. Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
“加入”这个名字不好听。 NHibernate 映射中的“连接”意味着基于两个表的主键关系的零到一关系。例如,如果您有一个 User 表和一个 UserAdditionalInfo 表,并且每个用户有零个或一个记录,则可以使用联接。 UserAdditionalInfo 表可能会引用来自 User 的 PK 作为外键及其自己的主键。当 DBA 必须认真维护旧应用程序的架构,但较新的应用程序需要相同概念记录的新字段时,这种情况很常见。
在您的情况下,您实际需要的是引用关系,其中一条记录与零个或一个其他记录具有外键关系。您可以像这样流畅地设置它:
问题是现在必须映射类别;它是一个独立的实体,现在与您的实体相关。您的选择是接受此模型,通过将实体引用设置为私有、更改映射以访问实体本身并将“传递”编码为您想要公开的属性来“扁平化”它,或者使用代码像 AutoMapper 这样的工具可以在运行时将此深层域模型投影到平面 DTO 中以供一般使用。它们都有优点和缺点。
"Join" is poorly named. a "join" in an NHibernate mapping implies a zero-to-one relationship based on a relation of the primary keys of the two tables. You would use a join if, for instance, you had a User table and a UserAdditionalInfo table, with zero or one record per User. The UserAdditionalInfo table would likely reference the PK from User as both a foreign key and its own primary key. This type of thing is common when a DBA has to religiously maintain a schema for a legacy app, but a newer app needs new fields for the same conceptual record.
What you actually need in your situation is a References relationship, where a record has a foreign key relationship to zero or one other records. You'd set it up fluently like so:
The problem with this is that Category must now be mapped; it is a separate entity which is now related to yours. Your options are to live with this model, to "flatten" it by making the entity reference private, changing the mapping to access the entity as such, and coding "pass-throughs" to the properties you want public, or by using a code tool like AutoMapper to project this deep domain model into a flat DTO at runtime for general use. They all have pros and cons.