【发布时间】:2014-10-13 07:52:42
【问题描述】:
我有两个基于两个视图的实体。映射如下所示:
实体 A:
<class name="SearchView" table="SearchView" dynamic-update="true" mutable="false" schema-action="none">
<id name="Id" type="Guid" column="Id" />
<property name="Id" column="Id" type="Guid" />
<property name="Expires" column="Expires" type="DateTime" />
<property name="VerificationNumber" column="VerificationNumber" type="Int32" />
<property name="InvoiceNo" column="InvoiceNo" type="Int32" length="50" />
<property name="Status" column="FakturaStatus" type="Int32" />
</class>
实体 B:
<class name="SearchInvoiceResourceLookUpView" table="SearchInvoiceResourceLookUpView" dynamic-update="true" mutable="false" schema-action="none">
<id name="Id" type="Guid" column="Id" />
<property name="InvoiceId" column="InvoiceId" type="Guid" />
<property name="ResourceId" column="ResourceId" type="Guid" />
</class>
实体 A 基于表视图,该表视图是更复杂表结构的平面视图,用于搜索优化。现在我希望能够从 Entity A 中获取所有行,其中 ID 位于 Entity B 中的“InvoiceId”列中,以获取 Entity B 中“ResourceId”的特定值strong>Entity B 使用 NHibernate 和 Criteria-API。这两个表都是视图,它们没有声明的关系。我在 C# 中尝试过以下代码,但它不起作用:
var criteria = _session.CreateCriteria(typeof(SearchView));
criteria.CreateAlias("SearchInvoiceResourceLookUpView", "srf",JoinType.InnerJoin)
.Add(Restrictions.EqProperty("sfr.InvoiceId", "Id"))
.Add(Restrictions.Eq("sfr.ResourceId", invoiceResId));
用于此目的的原始 SQL 将是:
SELECT * FROM SearchView
JOIN SearchInvoiceResourceLookUpView srf on srf.InvoiceId = Id
WHERE srf.ResourceId = '[Inser resource id here]'
我该如何解决?
还有其他更好的方法吗?
【问题讨论】:
标签: c# sql .net nhibernate icriteria