hibernate - 使用左连接获取重复

标签 hibernate jpa hql left-join fetch

我正在使用 HQL 查询来获取某些记录。如果我使用 LEFT JOIN FETCH ,目标实体中的集合将包含重复记录。如果我只使用左连接,则不会。我想当 Hibernate 延迟加载记录时,它可以避免重复。

"SELECT z FROM ", TableA.TABLE_NAME, " z ",  //
            "LEFT JOIN FETCH z.attributeX pv ", //
            "LEFT JOIN FETCH pv.attributeY anteil ", //
            "LEFT JOIN FETCH z.attributeB kma ", //
            "LEFT JOIN FETCH kma.attributeC ", //
            "WHERE anteil.attributeD.attributeE.id = :eId ", //
            "AND pv.attributeG.id = :gId ");

我的实体TableA有一个指向TablePV的链接(LEFT JOIN FETCH z.attributeX pv)

TablePV 具有 TableY 的集合(LEFT JOIN FETCH pv.attributeY anteil)

现在,Hibernate 将正确映射除 TablePV 的子项之外的所有内容。它将包含多次相同的记录。 TableA 上的不同没有帮助,因为那里没有重复项。我可以手动删除那些我认为性能很差的记录。

最佳答案

真正保证的唯一方法是在这些集合上使用 SetSortedSet 而不是使用 List。官方没有其他方法可以使用 Hibernate 来避免此问题:

@OneToMany
private Set<AttributeY> attributeY;

您可以在旧的 Hibernate 中阅读此提示 documentation :

Queries that make use of eager fetching of collections usually return duplicates of the root objects, but with their collections initialized. You can filter these duplicates through a Set.

或者对 newer one 上相同问题的某种引用:

The only difference is that Set doesn’t allow duplicates, but this constraint is enforced by the Java object contract rather than the database mapping.

设置和订购

如果您想使用Set并控制实体的顺序,you can use SortedSet 并在子实体上实现 Comparable:

@OneToMany
@SortNatural
private SortedSet<AttributeY> attributeY = new TreeSet<>();

还有:

@Entity
public class AttributeY implements Comparable<AttributeY> {

    @Override
    public int compareTo(AttributeY o) {
        return number.compareTo( o.getNumber() );
    }

}

要自定义排序逻辑,您可以使用@SortComparator

注意事项

如果没有更多细节,很难说为什么在某些情况下使用 List 会发生这种情况,而在另一些情况下却不会。但是您可以尝试使用实体的“业务 key ”来实现 equals/hashCode 方法:

When using sets, it’s very important to supply proper equals/hashCode implementations for child entities. In the absence of a custom equals/hashCode implementation logic, Hibernate will use the default Java reference-based object equality which might render unexpected results when mixing detached and managed object instances.

此外,您还使用 FETCH 别名 pvanteil 应用条件。 Don't do that 。并摆脱 JPQL 上的“火车残骸”(anteil.attributeD.attributeE.id),因为这会使 Hibernate 创建奇怪的 SQL(例如多次执行相同的 JOIN 或无效的 SQL) 。因此,请明确连接,而不是在 WHERE 上使用 FETCH 别名:

LEFT JOIN FETCH z.attributeX
LEFT JOIN FETCH pv.attributeY
LEFT JOIN FETCH z.attributeB kma
LEFT JOIN FETCH kma.attributeC
LEFT JOIN pv.attributeY anteil
LEFT JOIN anteil.attributeD attributeD
LEFT JOIN attributeD.attributeE attributeE
LEFT JOIN z.attributeX pv
LEFT JOIN pv.attributeG attributeG
WHERE attributeE.id = :eId 
AND attributeG.id = :gId

如果重复项位于根实体 TableA 中,则 DISTINCT 会有所帮助,但这不是您的情况。

关于hibernate - 使用左连接获取重复,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9720452/

相关文章:

MySQL 到 HQL 查询

java - 像oracle中的搜索一样吗?

hibernate - 如何将动态 @PersistenceContext 与 Hibernate 和 JBoss AS 7 连接池一起使用

hibernate - 带有Criteria的Grails,或者一种按预定顺序排序的快速方法

java - 如何使用 DataNucleus 查询/保留 joda-time DateTime 字段

java - Hibernate 中的 JPA 2.1 NamedSubgraph 忽略子类

java - 基于 JPA 的类的命令行

java - setMaxResults(max_size) 抛出 Sql 命令未正确结束的异常

java - HQL 不返回结果,但生成的 SQL 在 SQL Developer 上完美运行

java - 10 位 BigDecimal 无法保存在 NUMBER(10, 2) 列中