3

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; } 

1 Answer 1

4

You need to add an Alias or Criteria

session.CreateCriteria<Post>() .CreateAlias("Tags", "tag") .Add(Restrictions.Eq("tag.Id", tag.Id)) .List<Post>(); 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.