Milad Khajavi Milad Khajavi - 1 month ago 24
Java Question

Join two table using Criteria in Hibernate?

I have query like this written in hql format:

Query qq = session.createQuery(
"select question from Question question " +
"join question.information.tags t1 " +
"join question.information.tags t2 " +
"where t1.name = 'java' and t2.id = 63"
);


I wrote this query in Criteria format but it trow exception:

session.createCriteria(Question.class, "q")
.createAlias("q.information.tags", "t1")
.createAlias("q.information.tags", "t2")
.add(Restrictions.eq("t1.id", "63"))
.add(Restrictions.eq("t2.name", "java"))
.list();



Method threw 'org.hibernate.QueryException' exception.:
duplicate association path: information.tags


What is the problem? Any way to write hql in Criteria format?

Answer

I don't think it is possible to join the same association twice with Criteria

You can find the related JIRA here that is still open.