Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
556 views
in Technique[技术] by (71.8m points)

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

Entity 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'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;
    }

I'm facing the following exception:

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

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

relations in DB

database relations

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.

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;
}

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

More information about this topic:

Hibernate cannot simultaneously fetch multiple bags


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...