java - Hibernate 字段多个别名

标签 java hibernate spring postgresql

我有一个实体,它有集合字段(参数)。可以有很多参数。我想为每个独特的参数进行独特的加入。即 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/

相关文章:

java - 在 SWT 中设置/获取 RadioGroupFieldEditor 的值

java - 如何解析计算器输入字符串

java - 乐观锁定 - Hibernate 和 EJB - 使用 HQL 批量更新

java - 同步方法与同步块(synchronized block)

java - JPA 分离和立即合并导致错误

java - 是否可以在 tomcat 7 中的同一端口上运行多个 Web 应用程序

java - Spring mvc 异常中的 servlet 名称为空

java - Spring bean 范围 : session and globalSession

Spring 4 WebSocket + sockJS :. 在跟踪中获取 "No matching method found"且未调用 @MessageMapping 处理程序

java - 构建一个只能执行的JAR文件?