It looks like a Criteria query is duplicating joins under circumstances that I don't understand.
I create a Criteria query like this: Criteria criteria = this.currentSession().createCriteria(MyEntity.class); criteria.createAlias("join0", "j0", JoinType.LEFT_OUTER_JOIN); criteria.setFetchMode("j0", FetchMode.JOIN); criteria.createAlias("j0.join1", "j1", JoinType.LEFT_OUTER_JOIN); criteria.setFetchMode("j1", FetchMode.JOIN); criteria.createAlias("join2", "j2"); criteria.createAlias("j2.join3", "j3"); criteria.add(Restrictions.eq("j3.id", someInput)); criteria.createAlias("join4", "j4"); criteria.createAlias("j4.join5", "j5"); criteria.add(Restrictions.in("j5.id", someOtherInput)); criteria.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY); List<MyEntity> myEntities = criteria.list();
When I look at the criteria.toString() just before the .list(), It looks like this:
CriteriaImpl(com.myco.model.MyEntity:this[Subcriteria(join0:j0), Subcriteria(j0.join1:j1), Subcriteria(join2:j2), Subcriteria(j2.join3:j3), Subcriteria(join4:j4), Subcriteria(j4.join5:j5)][j3.id=19, j5.id in (459, 460, 479, 496, 497, 537, 538, 561, 576, 594, 537)])
But when the query executes, it duplicates the j4 and j5 joins and I get the following error: Caused by: java.sql.SQLException: The correlation name 'j45_' is specified multiple times in a FROM clause.
This is a simplified version of the query that is run. I eliminated the selection list and a union subclass to make it easier to read. select [snip] from MyEntity myEntity0_ on join23_.myEntityId=myEntity0_.id left outer join join0 j01_ on this_.id=j01_.myEntityId left outer join join1 j12_ on j01_.id=j12_.join1Id inner join join4 j45_ on this_.id=j45_.myEntityId inner join join5 j56_ on j45_.join5Id=j56_.id inner join join4 j45_ on this_.id=j45_.myEntityId inner join join5 j56_ on j45_.join5Id=j56_.id where join34_.id=? and j56_.id in (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
You may notice that both of the latter two joins are duplicated.
The code that defines the query is short and I know that I am not creating duplicate aliases to the same association, which seems to be verified when I look at the status of the criteria.toString() just before calling .list().
I am currently using Hibernate 4.2.3.Final, but I tried upgrading to 4.2.5.Final with identical results. Has anyone else seen anything like this before?
|