Hibernate:org.hibernate.loader.MultipleBagFetchException:无法同时获取多个包 [英] Hibernate : org.hibernate.loader.MultipleBagFetchException: cannot simultaneously fetch multiple bags

查看:51
本文介绍了Hibernate:org.hibernate.loader.MultipleBagFetchException:无法同时获取多个包的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

实体类

客户

       @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>();
//...

ShippingAdress

    @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>();
    //...

CustomerOrder

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


    @Id
    @ManyToOne

    private Items item;

当我尝试获取特定客户的所有订单

When I'm trying to get all orders from particular customer

  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 得到以下内容:(假定已从客户中删除)

If delete fetch = FetchType.EAGER from one of the places they are existed getting the following: (supposed deleted from customer )

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

数据库中的关系

推荐答案

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

As the exception says to you, you can't fetch two related collections or bags simultaneously. A quick solution would be to remove the FetchType.EAGER from one collection and force the fetching of that collection explicitly calling the collection objects.

例如,如果从 shippingAddress 集合中删除 FetchType.EAGER ,则可以这样强制执行:

For example, if you remove the FetchType.EAGER from shippingAddress collection, you can force the fetching like this:

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()方法将强制获取该集合的所有元素.

Calling the size() method of the collection will force the fetching of all the elements of that collection.

有关此主题的更多信息:

More information about this topic:

Hibernate无法同时获取多个包

这篇关于Hibernate:org.hibernate.loader.MultipleBagFetchException:无法同时获取多个包的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆