java hibernate 包_java - Hibernate无法同时获取多个包

Hibernate在SessionFactory创建期间抛出此异常:

org.hibernate.loader.MultipleBagFetchException:无法同时获取多个行李

这是我的测试用例:

Parent.java

@Entity

public Parent {

@Id

@GeneratedValue(strategy=GenerationType.IDENTITY)

private Long id;

@OneToMany(mappedBy="parent", fetch=FetchType.EAGER)

// @IndexColumn(name="INDEX_COL") if I had this the problem solve but I retrieve more children than I have, one child is null.

private List children;

}

Child.java

@Entity

public Child {

@Id

@GeneratedValue(strategy=GenerationType.IDENTITY)

private Long id;

@ManyToOne

private Parent parent;

}

这个问题怎么样? 我能做什么?

编辑

好吧,我遇到的问题是另一个“父”实体在我父母的内部,我的真实行为是这样的:

Parent.java

@Entity

public Parent {

@Id

@GeneratedValue(strategy=GenerationType.IDENTITY)

private Long id;

@ManyToOne

private AntoherParent anotherParent;

@OneToMany(mappedBy="parent", fetch=FetchType.EAGER)

private List children;

}

AnotherParent.java

@Entity

public AntoherParent {

@Id

@GeneratedValue(strategy=GenerationType.IDENTITY)

private Long id;

@OneToMany(mappedBy="parent", fetch=FetchType.EAGER)

private List anotherChildren;

}

Hibernate不喜欢FetchType.EAGER的两个集合,但这似乎是一个bug,我没有做不寻常的事情......

从Parent或AnotherParent删除FetchType.EAGER解决了这个问题,但我需要它,所以真正的解决方案是使用@LazyCollection(LazyCollectionOption.FALSE)而不是FetchType(感谢Bozho的解决方案)。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值