Search code examples
hibernatecriteriaquery-by-example

Hibernate Criteria by Examle


I am trying to create Criteria by an Example. But I am getting the same results for the different Example objects.

Author author = new Author();
author.setId(3L);
News news = new News();
news.setAuthor(author);

List<News> newsList = getSession().createCriteria(News.class)
           .add(Example.create(news))
           .list();

If I change the id, then I get the the same news list. I tried to do next:

//...
.add(Example.create(news.getAuthor())

But it doesn't work too. I know that I can use for example Restrictions or HQL, but I would like to try do it like that. How to use the Examle correctly?


Solution

  • From the documentation

    Version properties, identifiers and associations are ignored. By default, null valued properties are excluded.