Reputation: 175
I am having an entity, which has collections field (params). There can be many params. I want to make unique join for each unique param. i.e. p1.id = ? AND p2.id = ? AND p3.id = ?
etc.
In Hibernate, when I try to create alias for the same field, it throws exception with message about duplicate alias.
How can I achieve multiple joins for the field?
I am using spring framework, hibernate and postgresql.
Thanks in advance.
Upvotes: 4
Views: 4229
Reputation: 30528
I assume that you are using the Criteria API.
If you check the source code of Hibernate you can find the problem here:
CriteriaQueryTranslator#createAssociationPathCriteriaMap()
private void createAssociationPathCriteriaMap() {
Iterator iter = rootCriteria.iterateSubcriteria();
while ( iter.hasNext() ) {
CriteriaImpl.Subcriteria crit = ( CriteriaImpl.Subcriteria ) iter.next();
String wholeAssociationPath = getWholeAssociationPath( crit );
Object old = associationPathCriteriaMap.put( wholeAssociationPath, crit );
if ( old != null ) {
throw new QueryException( "duplicate association path: " + wholeAssociationPath );
}
int joinType = crit.getJoinType();
old = associationPathJoinTypesMap.put( wholeAssociationPath, new Integer( joinType ) );
if ( old != null ) {
// TODO : not so sure this is needed...
throw new QueryException( "duplicate association path: " + wholeAssociationPath );
}
}
}
I'm using Hibernate 3.3.2.
What happens internally is that When you are adding Criteria
s to your root Criteria
Hibernate creates SubCriteria
s and later fills a Map
(check the code below) with all the paths you are trying to use and if it finds a duplication it will throw an exception.
So for example if you are trying to join: entity0.entity1
and later you try to join that again you will get an Exception
. It won't work even with an alias because Hibernate doesn't care about them here.
You can work around this if you don't join something twice OR you can use HQL/JPQL. You may check out newer versions of Hibernate but I'm not sure if they fixed this quasi-bug.
Upvotes: 2