开发者

NHibernate one-to-many criteria query

开发者 https://www.devze.com 2023-02-04 18:03 出处:网络
Suppose I have a Post class and a Tag class. The relationship between Post and Tag is 开发者_开发问答one-to-many. How can I write a Hibernate query to retrieve a List of Post objects which have a give

Suppose I have a Post class and a Tag class. The relationship between Post and Tag is 开发者_开发问答one-to-many. How can I write a Hibernate query to retrieve a List of Post objects which have a given Tag?

public IList<Post> FindByTag(Tag tag)
{
    IList<Post> posts;
    using (ISession session = HibernateUtil.GetSessionFactory().OpenSession())
    {
        posts = session.CreateCriteria<Post>()
            .Add(...) // what Criteria do I add?
            .List<Post>();
    }
    return posts;
}


You need to add an Alias or Criteria

session.CreateCriteria<Post>()
.CreateAlias("Tags", "tag")
.Add(Restrictions.Eq("tag.Id", tag.Id))
.List<Post>();
0

精彩评论

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