Hibernate:org.hibernate.loader.MultipleBagFetchException:不能同时获取多个包

Posted

技术标签:

【中文标题】Hibernate:org.hibernate.loader.MultipleBagFetchException:不能同时获取多个包【英文标题】:Hibernate : org.hibernate.loader.MultipleBagFetchException: cannot simultaneously fetch multiple bags 【发布时间】:2016-09-13 22:49:03 【问题描述】:

实体类

客户

       @Entity
        @Table(name="Custumer")
        public class Custumer implements Serializable 

            @Id
            @GeneratedValue(strategy = GenerationType.IDENTITY)
            @Column(name="id")
            private Long id;
            @Column(name="name",unique = true)
            private String name;
            @Column(name="Email",unique = true)
            private String Email;
            @Column(name = "Coins")
            private Double coins;

            @ManyToMany(fetch = FetchType.EAGER,cascade = CascadeType.ALL)

            private List<ShippingAdress> shippingAdress = new ArrayList<ShippingAdress>();


            @OneToMany( fetch = FetchType.EAGER, mappedBy = "custumer", cascade = CascadeType.ALL,orphanRemoval = true)

            private List<Order> orders= new LinkedList<Order>();
//...

送货地址

    @Entity
        @Table(name="ShippingAdress")
        public class ShippingAdress implements Serializable 

            @Id
            @GeneratedValue(strategy = GenerationType.IDENTITY)
            @Column(name="id")
            private Long id;
            @Column(name="postCode")
            private String PostCode;
            @Column(name="street")
            private String street;
            @Column(name="house")
            private String house;
            @Column(name="flat")
            private String flat;

            @ManyToMany(mappedBy = "shippingAdress")
            private List<Custumer> custumers = new LinkedList<Custumer>();
    //...

项目

@Entity
    @Table(name="Items")
    public class Items implements  Serializable,Comparable<Items>  

        @Id
        @GeneratedValue(strategy = GenerationType.IDENTITY)

        @Column(name="id")
        private Long id;
        @Column(name="name", unique = true)
        private String name;
        @Column(name="price")
        private double price;
        @Column(name="count")
        private int count;

        @OneToMany(mappedBy = "item", cascade = CascadeType.ALL,orphanRemoval = true)
        List<Order> orders = new LinkedList<Order>();
    //...

客户订单

@Entity
@Table(name = "CustumerOrder")
public class Order implements Serializable 
    @Id
    @ManyToOne
    private Custumer custumer;


    @Id
    @ManyToOne

    private Items item;

当我试图从特定客户那里获得所有订单时

  public List<Order> getOrderByCustumerId(Custumer custumer) 

        Criteria criteria = session.createCriteria(Order.class);
        List<Order> res = (List<Order>) criteria.add(Restrictions.eq("custumer",custumer)).list();

        return res;
    

我面临以下异常:

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

如果从它们存在的地方之一删除fetch = FetchType.EAGER 得到以下信息: (假设从客户那里删除)

   org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: org.CoinsShop.DataBase.DataSets.Custumer.shippingAdress, could not initialize proxy - no Session

数据库中的关系

【问题讨论】:

请看这篇帖子***.com/questions/17566304/… 【参考方案1】:

正如例外对您所说,您不能同时获取两个相关的收藏品或包。一个快速的解决方案是从一个集合中删除 FetchType.EAGER 并强制获取该集合,显式调用集合对象。

例如,如果您从shippingAddress 集合中删除FetchType.EAGER,您可以像这样强制提取:

public List<Order> getOrderByCustomerId(Customer customer) 
    Criteria criteria = session.createCriteria(Order.class);
    List<Order> res = (List<Order>) criteria.add(Restrictions.eq("customer",customer)).list();

    for (Order order : res)
        order.getCustomer().getShippingAddress().size();

    return res;

调用集合的size() 方法将强制获取该集合的所有元素。

有关此主题的更多信息:

Hibernate cannot simultaneously fetch multiple bags

【讨论】:

以上是关于Hibernate:org.hibernate.loader.MultipleBagFetchException:不能同时获取多个包的主要内容,如果未能解决你的问题,请参考以下文章

Spring和Hibernate的注解整合 hibernate3和hibernate4/5的区别

hibernate.merge()方法怎么用

hibernate 异常 怎么解决

Hibernate之Hibernate环境配置

(转)Hibernate框架基础——Hibernate API及Hibernate主配置文件

Hibernate基础学习—Hibernate相关API介绍