欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

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

相关标签: jpa jpa