Hibernate: org.hibernate.loader.MultipleBagFetchException: cannot retrieve multiple packages at the same time

Feature Classes

Customer

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

Items

@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 try to get all orders from a specific 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;
    }

I came across the following exception:

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

If delete is fetch = FetchType.EAGERfrom one of the places where they exist, receiving the following: (presumably removed from the client)

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

in DB

relationship with database

+4
source share
1 answer

As indicated in the exception, you cannot take two related collections or bags at the same time. A quick fix would be to remove the FetchType.EAGERcollection from the same collection and force the collection to explicitly invoke the objects in the collection.

, FetchType.EAGER shippingAddress, :

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

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

    return res;
}

size() .

:

Hibernate

+3

All Articles