Hibernate Multi-User Alias

I have an object that has a collection field (params). There can be many options. I want to make a unique connection for each unique parameter. those. p1.id = ? AND p2.id = ? AND p3.id = ? etc.

In Hibernate, when I try to create an alias for the same field, it throws an exception with a message about a duplicate alias.

How can I get multiple connections for a field?

I use spring, hibernate, and postgresql frameworks.

Thanks in advance.

+4
source share
1 answer

I assume that you are using the criteria API.

If you check the source code for Hibernate, you may 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 am using Hibernate 3.3.2.

What happens inside is that when you add Criteria to your root, Criteria Hibernate creates a SubCriteria and then populates the Map (check the code below) with all the paths you are trying to use, and if it detects duplication, that will throw an exception.

So, for example, if you try to join: entity0.entity1 , and later you try to join this, you will get an Exception . It will not work even with an alias, because Hibernate does not care about them here.

You can get around this if you don't join something twice, or you can use HQL / JPQL. You can check newer versions of Hibernate, but I'm not sure if they fixed this quasi error.

+2
source

Source: https://habr.com/ru/post/1396180/


All Articles