jpa cannot simultaneously fetch multiple bags OneToMany
程序员文章站
2022-03-02 15:39:37
...
@OneToMany(targetEntity = PacketPrize.class, fetch = FetchType.EAGER)
@JoinColumn(name = "packetId", referencedColumnName = "id",
insertable = false, updatable = false,
foreignKey = @ForeignKey(value = ConstraintMode.NO_CONSTRAINT))
private List<PacketPrize> packetPrizes;
@OneToMany(targetEntity = NewUserPacketDetail.class, fetch = FetchType.LAZY)
@JoinColumn(name = "packetId", referencedColumnName = "id",
insertable = false, updatable = false,
foreignKey = @ForeignKey(value = ConstraintMode.NO_CONSTRAINT))
private List<NewUserPacketDetail> packetDetails;
在用JPA进行注释时,如果一个实体里要映射多个集合实体时,我们不能把两个集合的的FetchType设置为EAGER,此时只能设置为LAZY,否则会报:cannot simultaneously fetch multiple bags。
但是有个问题,查询可能会报错:
Method threw 'org.hibernate.LazyInitializationException' exception. Cannot evaluate com.**.activitys.model.***.toString()
service层加 @Transactional 可以查出来。但是controller还报错
、、还没解决
又出现一个问题,这样controller没问题,但是页面加载还是会报错
could not initialize proxy - no Session
这个时候添加配置:
spring.jpa.open-in-view=true
参考:https://blog.csdn.net/johnf_nash/article/details/80658626
推荐阅读
-
2010.04.08———cannot simultaneously fetch multiple bags异常
-
jpa cannot simultaneously fetch multiple bags OneToMany
-
Hibernate抓取策略以及如何避免cannot simultaneously fetch multiple bags异常
-
JPA的cannot simultaneously fetch multiple bags异常的解决
-
使用Hibernate 过程中 报—— cannot simultaneously fetch multiple bags
-
hibernate exception (cannot simultaneously fetch multiple bags)
-
“cannot simultaneously fetch multiple bags”的解决方法