nHibernate标准,用于在集合中的子项具有特定值时选择父项 [英] nHibernate Criteria for selecting a parent if a child in a collection has a specific value

查看:53
本文介绍了nHibernate标准,用于在集合中的子项具有特定值时选择父项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我具有以下类结构,如果其中一个孩子的孩子有一个特定的名字,那么选择父母的NHibernate条件是什么?

If I have the following class structure what is the NHibernate criteria to select a parent if one of it's children has a specific name?

 public class Child
 {
     public int Id { get; set; }
     public int Name { get; set; }
 }

 public class Parent
 {
     public int Id { get; set; }
     public IList<Child> Children { get; set; }
 }

推荐答案

我只是为集合创建一个别名并添加限制.

I'd just create an alias to the collection and add restrictions.

var parentsWithKidName = session.CreateCriteria<Parent>()
    .CreateAlias("Children", "c", JoinType.InnerJoin)
    .Add(Restrictions.Eq("c.Name", childName))
    .SetResultTransformer(Transformers.DistinctRootEntity()) 
    .List<Parent>();

这将导致

select p.* 
from parent p 
inner join child c on /* however it's mapped? */
where c.Name = ?

不同的根实体转换器将处理结果集并删除重复的父代.他们仍然碰到了电线.

The distinct root entity transformer will process the result set and remove duplicated parents. They still come across the wire though.

这篇关于nHibernate标准,用于在集合中的子项具有特定值时选择父项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆