我有一个实体,它有集合字段(参数)。可以有很多参数。我想为每个独特的参数进行独特的加入。即 p1.id = ?和 p2.id =? AND p3.id = ?
等
在 Hibernate 中,当我尝试为同一字段创建别名时,它会抛出异常并显示有关重复别名的消息。
如何实现该字段的多个连接?
我正在使用 spring 框架、hibernate 和 postgresql。
提前致谢。
最佳答案
我假设您正在使用 Criteria API。
如果您检查 Hibernate 的源代码,您可以在此处找到问题:
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 );
}
}
}
我正在使用 Hibernate 3.3.2。
内部发生的事情是,当您将 Criteria
添加到根 Criteria
时,Hibernate 创建 SubCriteria
并稍后填充 映射
(检查下面的代码)您尝试使用的所有路径,如果发现重复,它将抛出异常。
例如,如果您尝试加入:entity0.entity1
,稍后您尝试再次加入,您将得到一个Exception
。它甚至不会使用别名,因为 Hibernate 在这里不关心它们。
如果您不两次加入某些内容,或者您可以使用 HQL/JPQL,则可以解决此问题。您可以查看较新版本的 Hibernate,但我不确定他们是否修复了这个准错误。
关于java - Hibernate 字段多个别名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9260080/