java - org.hibernate.loader.MultipleBagFetchException : cannot simultaneously fetch multiple bags

标签 java hibernate jpa hibernate-mapping bag

以下是我的代码在这里,我使用多个列表从数据库中获取数据。 从 hql 查询中获取数据时显示异常。

Pojo 类

public class BillDetails implements java.io.Serializable {

private Long billNo;
// other fields
@LazyCollection(LazyCollectionOption.FALSE)
private List<BillPaidDetails> billPaidDetailses = new ArrayList<BillPaidDetails>();
private Set productReplacements = new HashSet(0);
@LazyCollection(LazyCollectionOption.FALSE)
private List<BillProduct> billProductList = new ArrayList<BillProduct>();
//getter and setter
}

hmb.xml文件

<class name="iland.hbm.BillDetails" table="bill_details" catalog="retail_shop">
        <id name="billNo" type="java.lang.Long">
            <column name="bill_no" />
            <generator class="identity" />
        </id>
 <bag name="billProductList" table="bill_product" inverse="true" lazy="false" fetch="join">
            <key>
                <column name="bill_no" not-null="true" />
            </key>
            <one-to-many class="iland.hbm.BillProduct" />
        </bag>
        <bag name="billPaidDetailses" table="bill_paid_details" inverse="true" lazy="false" fetch="select">
            <key>
                <column name="bill_no" not-null="true" />
            </key>
            <one-to-many class="iland.hbm.BillPaidDetails" />
        </bag>
        <set name="productReplacements" table="product_replacement" inverse="true" lazy="false" fetch="join">
            <key>
                <column name="bill_no" not-null="true" />
            </key>
            <one-to-many class="iland.hbm.ProductReplacement" />
        </set>
    </class>

Hql查询

String hql = "select distinct bd,sum(bpds.amount) from BillDetails as bd "
                    + "left join fetch bd.customerDetails as cd "
                    + "left join fetch bd.billProductList as bpd "
                    + "left join fetch bpd.product as pd "
                    +"left join fetch bd.billPaidDetailses as bpds "
                    + "where bd.billNo=:id "
                    + "and bd.client.id=:cid ";

我正在尝试按照查询从数据库中获取数据,但这显示 org.hibernate.loader.MultipleBagFetchException:无法同时获取多个包 如何解决这个问题

最佳答案

Hibernate 不允许获取多个包,因为那样会生成 Cartesian product .

现在,您会找到很多答案、博客文章、视频或其他资源,告诉您使用Set 而不是List 来创建您的集合。

这是个糟糕的建议!

使用 Sets 而不是 Lists 将使 MultipleBagFetchException 消失,但笛卡尔积仍会存在。

正确的修复

而不是在单个 JPQL 或 Criteria API 查询中使用多个 JOIN FETCH:

List<Post> posts = entityManager.createQuery("""
    select p
    from Post p
    left join fetch p.comments
    left join fetch p.tags
    where p.id between :minId and :maxId
    """, Post.class)
.setParameter("minId", 1L)
.setParameter("maxId", 50L)
.getResultList();

您可以使用以下技巧:

List<Post> posts = entityManager.createQuery("""
    select distinct p
    from Post p
    left join fetch p.comments
    where p.id between :minId and :maxId
    """, Post.class)
.setParameter("minId", 1L)
.setParameter("maxId", 50L)
.setHint(QueryHints.PASS_DISTINCT_THROUGH, false)
.getResultList();

posts = entityManager.createQuery("""
    select distinct p
    from Post p
    left join fetch p.tags t
    where p in :posts
    """, Post.class)
.setParameter("posts", posts)
.setHint(QueryHints.PASS_DISTINCT_THROUGH, false)
.getResultList();

只要您使用JOIN FETCH 最多获取一个集合,就可以了。通过使用多个查询,您将避免使用笛卡尔积,因为除了第一个集合之外的任何其他集合都是使用辅助查询获取的。

关于java - org.hibernate.loader.MultipleBagFetchException : cannot simultaneously fetch multiple bags,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24675340/

相关文章:

java - 我如何检测Android中外部应用程序的点击事件?

java - 是否有一个 bean 验证注释用于检查字符串字段中的数据是否为数字?

java - 如何使用hibernate查询两个表(连接表)?

java - 如何使用 javadb JUnit 测试 jpa 代码而不修改原始数据库?

java - 如何从 Java 类中的另一个方法调用我的方法?

java - 为什么else语句是死循环?

java - Proguard 生成错误的字符串资源 ID

java - HQL连接函数

java - Hibernate 单表中复合外键和主键的一对一映射

java - 如何告诉JPA管理从属实体?