I want to get all questions marked by tags "one" and "two" with Criteria API:
Criteria criteria = sessionFactory.getCurrentSession().createCriteria(Question.class);
if (tags != null && tags.length > 0) {
criteria.createAlias("tags", "t", JoinType.INNER_JOIN);
Criterion[] criterions = new Criterion[tags.length];
for (int i = 0; i < tags.length; i++) {
criterions[i] = Restrictions.eq("t.name", tags[i]);
}
criteria.add(Restrictions.and(criterions));
}
return criteria.list();
When tags = ["one", "two"] nothing is returned, because, as I understand, hibernate firstly makes each row for each (question, tag) pair, then filters it, and only then grouping by Questions.
How can I fix this?
tags = ["one"] or tags = ["two"] works fine.