我有一个看起来像这样的课:
[Class(Table = "SessionReportSummaries", Mutable = false)]
public class SessionReportSummaries
{
[ManyToOne(Column = "ClientId", Fetch = FetchMode.Join)]
public Client Client { get; private set; }
[ManyToOne(Column = "ClientId", Fetch = FetchMode.Join)]
public ClientReportSummary ClientReportSummary { get; private set; }
}
Run Code Online (Sandbox Code Playgroud)
SessionReportSummaries视图有一个ClientId列,我正在尝试使用此列连接Client对象和ClientReportSummary对象.
不幸的是,NHibernate只想加入类中定义的第一个,并且总是为第二个执行SELECT.所以在这种情况下,NHibernate首先使用以下方法查询数据库:
SELECT {stuff} FROM SessionReportSummaries ... left outer join Clients on this.ClientId=Clients.Id ...
Run Code Online (Sandbox Code Playgroud)
(有很多其他连接),然后N这些:
SELECT {stuff} FROM ClientReportSummary WHERE ClientReportSummary.ClientId = '{id goes here}'
Run Code Online (Sandbox Code Playgroud)
每个N个客户都有一个.这导致了糟糕的表现.
如果我交换Client和ClientReportSummary对象的位置,那么NHibernate会将ClientReportSummary连接到SessionReportSummaries对象上,并为每个Client对象执行select.
有谁知道如何让NHibernate为这两者进行连接?
NHibernate 将在单个查询中仅采用一个相同的列映射。因此,因为有两个不同的实体通过列属性映射到值“ClientId”:
在这种情况下,不授予列映射的唯一性。当应用两个实体的插入或更新形式时,它可能会造成损坏。但我们可以使用一个技巧:FORMULA映射
[Class(Table = "SessionReportSummaries", Mutable = false)]
public class SessionReportSummaries
{
[ManyToOne(Column = "ClientId", Fetch = FetchMode.Join)]
public Client Client { get; private set; }
[ManyToOne(Formula = "ClientId", Fetch = FetchMode.Join)]
public ClientReportSummary ClientReportSummary { get; private set; }
}
Run Code Online (Sandbox Code Playgroud)
现在,NHibernate 将把一个列映射作为实际关系,并将第二个列映射(在 中定义formula)评估为不同的关系。现在将使用单个选择语句
每当formula用于映射(而不是column)时,应将其标记为insert="false"和update="false"。我们只需要它来进行 SELECT。(否则我们可以将具有不同 ClientId 的 Client 和 ClientReportSummary 附加到 SessionReportSummaries 实体 - 这将违反异常...
第二种方法可能是一对一映射,其中“ClientId”预计在所有三个表中实际上相同......但这是另一个主题
| 归档时间: |
|
| 查看次数: |
295 次 |
| 最近记录: |