开发者

nHibernate Criteria for selecting a parent if a child in a collection has a specific value

If I 开发者_C百科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>();

This would result in

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.


What I did was to create a criteria query for the parent type, use the return to create a criteria query for the child type, and then add the specific conditions to the child type sub query.

public virtual IList<T> GetByChildCriteria(string childName,
   params ICriterion[] criterion)
{
     ICriteria criteria = NHibernateSession
     .CreateCriteria(persitentType)
     .CreateCriteria(childName);
     foreach (ICriterion criterium in criterion)
     {
         criteria.Add(criterium);
     }
     return criteria.List<T>();
}

Note: The NHibernateSession variable is of type ISession.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜