Java Hibernate使用多对多关系和左连接多次加载同一实体?

Java Hibernate使用多对多关系和左连接多次加载同一实体?,java,spring,spring-boot,hibernate,jpa,Java,Spring,Spring Boot,Hibernate,Jpa,我试图用它的类别加载单个商户实体,并且商户和类别之间的关系是多对多的。类别实体也有翻译。。。所以我编写了以下代码来从数据库中检索所需的数据: Merchant merchant = queryFactory.selectFrom(QMerchant.merchant) .leftJoin(QMerchant.merchant.categories, QMerchantCategory.merchantCategory).fetchJoin() .leftJoin(

我试图用它的类别加载单个商户实体,并且商户和类别之间的关系是多对多的。类别实体也有翻译。。。所以我编写了以下代码来从数据库中检索所需的数据:

Merchant merchant = queryFactory.selectFrom(QMerchant.merchant)
        .leftJoin(QMerchant.merchant.categories, QMerchantCategory.merchantCategory).fetchJoin()
        .leftJoin(QMerchantCategory.merchantCategory.translations, QMerchantCategoryTranslation.merchantCategoryTranslation).fetchJoin()
        .where(QMerchant.merchant.id.eq(merchantStore.getMerchant().getId()))
        .fetchOne();
然而,我得到的结果是商人有三个类别(相同)的列表,每个类别有三个翻译对象

我可以在数据库中检查我试图加载的merchant replationship中只有一个类别,正如您从JVM ID的图片中看到的,这些都是相同的实例。。。以下代码也给出了相同的结果:

@EntityGraph(attributePaths = {"categories", "categories.translations"})
Optional<Merchant> findWithCategoriesById(long id);
@EntityGraph(attributePath={“categories”,“categories.translations”})
可选findWithCategoriesById(长id);
如果添加另一个翻译,那么我将有四个类别的商人


那么,我如何告诉hibernate加载列表中有一个类别实体的商户实体(它应该有,因为只有一个类别与商户关联),但我还需要加载该类别的所有翻译,如果可能,要用一个查询完成,我可以用两个查询解决,但我希望只有一个。。。或者,如果有更好的理由使用两个查询来从数据库提取数据…?

使用
@OneToMany(fetch=FetchType.LAZY)
注释和LAZY fetch type方法。这是一篇好文章。

问题在于
categories
属性是一个列表。您需要使用
来代替

我使用的是FetchType.LAZY everywhere,这不是我不想加载的问题,问题是hibernate duplicates categories…这是否回答了您的问题?