我有一个实体,它有收集字段(params).可以有许多参数.我想为每个独特的参数进行独特的连接.即p1.id =? AND 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.
内部发生的事情是,当您将Criterias添加到根条件时,Hibernate会创建SubCriterias,然后使用您尝试使用的所有路径填充Map(检查下面的代码),如果找到重复,它将引发异常.
因此,例如,如果您尝试加入:entity0.entity1,稍后您尝试再次加入,您将获得一个例外.即使使用别名也无法工作,因为Hibernate在这里并不关心它们.
如果你不加入两次或者你可以使用HQL / JPQL,你可以解决这个问题.您可以查看更新版本的Hibernate,但我不确定他们是否修复了这个准bug.
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。