java 休眠:org.hibernate.loader.MultipleBagFetchException:无法同时获取多个包
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37282850/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Hibernate : org.hibernate.loader.MultipleBagFetchException: cannot simultaneously fetch multiple bags
提问by ketchyn
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 )
如果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
relations in DB
DB中的关系
回答by JMSilla
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.
正如例外告诉你的那样,你不能同时获取两个相关的集合或包。一个快速的解决方案是FetchType.EAGER
从一个集合中删除 ,并强制显式调用集合对象来获取该集合。
For example, if you remove the FetchType.EAGER
from shippingAddress
collection, you can force the fetching like this:
例如,如果您删除FetchType.EAGER
fromshippingAddress
集合,您可以像这样强制获取:
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.
调用size()
集合的方法将强制获取该集合的所有元素。
More information about this topic:
有关此主题的更多信息: