org.hibernate.loader.MultipleBagFetchException:无法同时获取多个行李

xrc*_*wrn 15 java hibernate jpa bag hibernate-mapping

以下是我的代码在这里,我使用多个列表从数据库中获取数据.从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
}
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)

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 ";
Run Code Online (Sandbox Code Playgroud)

我正在尝试跟随查询从数据库中获取数据,但这显示了 org.hibernate.loader.MultipleBagFetchException: cannot simultaneously fetch multiple bags 如何解决此问题

BER*_*ine 24

对我来说,我有同样的错误,我通过添加hibernate @Fetch的注释解决了

@OneToMany(mappedBy="parent", fetch=FetchType.EAGER)
@Fetch(value = FetchMode.SUBSELECT)
private List<Child> childs;
Run Code Online (Sandbox Code Playgroud)

  • 如果我想使用 fetch type Lazy 该怎么办 (3认同)

Vla*_*cea 22

正如本文所述,Hibernate不允许获取多个包,因为这会生成笛卡尔积.

您可以将行李更改为集合,并添加Set属性以"模拟"有序列表行为:

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();
Run Code Online (Sandbox Code Playgroud)

但只是因为你可以,这并不意味着你应该.

您可以做的是在原始SQL查询中最多获取一个集合,而之后使用二级查询获取其他集合.这样您就可以避免笛卡尔积.

另一种选择是使用从子实体到父实体的多级提取.

  • 我认为这篇文章与`set`s http://blog.eyallupu.com/2010/06/hibernate-exception-simultaneously.html的实际加载更相关.此外,将行李更改为集合不会使加入获取查询结果更少的笛卡儿产品,这本身可能是大型收藏品的问题. (2认同)