NHibernate query criteria

Suppose I have a Post class and a Tag class. The relationship between mail and tag is one-to-many. How do I write a Hibernate query to retrieve List of Post objects that have this 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;
}
+3
source share
1 answer

You need to add an alias or criteria

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

Source: https://habr.com/ru/post/1785664/


All Articles