【发布时间】:2013-04-19 09:43:40
【问题描述】:
我试图在没有单个根实体的情况下获取多个实体(实体关系的有向图只是一个弱连接图,不是强连接),我不知道该怎么做它在 Nhibernates QueryOver api(或 Linq,但这似乎更弱)
这些是我的关系:
ClientTaxEntity 引用 1 Client 和 N Manufacturers
InstructionTemplate 引用 1 Client 和 1 Manufacturer
我想获得所有可能的客户-制造商对的结果(可能 = 它们一起在 ClientTaxEntity 中),如果存在则为它们获取模板(否则为 null)
这是我迄今为止尝试过的:
Client client = null;
Manufacturer manufacturer = null;
InstructionTemplate template = null;
ClientTaxEntity taxEntity = null;
InstructionTemplateInfo info = null;
var query =Session.QueryOver<ClientTaxEntity>(() => taxEntity)
.JoinAlias(x => x.Client, () => client)
.JoinAlias(x => x.Manufacturers, () => manufacturer)
.Left
.JoinQueryOver(() => template, () => template,() => template.Client == client && template.Manufacturer == manufacturer);
var result = query
.SelectList(builder => builder
.Select(() => client.Name).WithAlias(() => info.ClientName)
.Select(() => client.Id).WithAlias(() => info.ClientId)
.Select(() => manufacturer.Name).WithAlias(() => info.ManufacturerName)
.Select(() => manufacturer.Id).WithAlias(() => info.ManufacturerId)
.Select(() => template.Id).WithAlias(() => info.TemplateId)
.Select(() => template.Type).WithAlias(() => info.Type)
)
.TransformUsing(Transformers.DistinctRootEntity)
.TransformUsing(Transformers.AliasToBean<InstructionTemplateInfo>())
.List<InstructionTemplateInfo>();
info 对象是我想要的结果。
但是语法 .JoinQueryOver(() => template 似乎对路径参数无效(异常表示:could not resolve property: template of: ClientTaxEntity
【问题讨论】:
-
我知道我可以在两个查询中做到这一点,因为我有两个根实体,但我正在寻找一个单一的数据库查询解决方案。
-
ClientTaxEntity和InstructionTemplate之间有关系吗? -
不,没有。我也尝试用子选择替换连接,但如果子选择的结果产生超过 1 个值,它会失败
标签: nhibernate queryover nhibernate-criteria