jpa 報錯: Unable to build Hibernate SessionFactory; nested exception is org.hibernate.loader.MultipleBagFetch


model代碼如下:

@OneToMany(fetch=FetchType.EAGER, cascade = CascadeType.ALL)
    @Where(clause="isValid=1 and orderType=1")
    @JoinColumn(name = "orderUUID", referencedColumnName = "JobShippingOrderUUID",insertable = false, updatable = false)
    public List<BoJobgoods> getBoJobgoodsList() {
        return boJobgoodsList;
    }

    @OneToMany(fetch=FetchType.EAGER, cascade = CascadeType.ALL)
    @Where(clause="isValid=1 and orderType=1")
    @JoinColumn(name = "orderUUID", referencedColumnName = "JobShippingOrderUUID",insertable = false, updatable = false)
    public List<BoJobcontainer> getBoJobcontainerList() {
        return boJobcontainerList;
    }

啟動就報錯 Unable to build Hibernate SessionFactory; nested exception is org.hibernate.loader.MultipleBagFetch。。。。。。。。。。

原因是同一個model里不能有二個 FetchType.EAGER 

於是把另一個改成:FetchType.LAZY 再次啟動成功

但是訪問接口又報另一個錯:

Could not write JSON: failed to lazily initialize a collection of role: com.kintech.model.domain.bo.BoJobshippingorder.boJobgoodsList, could not initialize proxy - no Session; nested exception is com.fasterxml.jackson.databind.JsonMappingException: failed to lazily initialize a collection of role: 

於是再改把后碼的list字段添加@Fetch(FetchMode.SUBSELECT)注解代碼如下:

@OneToMany(fetch=FetchType.EAGER)
    @Where(clause="isValid=1 and orderType=1")
    @JoinColumn(name = "orderUUID", referencedColumnName = "JobShippingOrderUUID",insertable = false, updatable = false)
    public List<BoJobgoods> getBoJobgoodsList() {
        return boJobgoodsList;
    }

    public void setBoJobgoodsList(List<BoJobgoods> boJobgoodsList) {
        this.boJobgoodsList = boJobgoodsList;
    }

    @OneToMany(fetch=FetchType.EAGER)
    @Fetch(FetchMode.SUBSELECT)
    @Where(clause="isValid=1 and orderType=1")
    @JoinColumn(name = "orderUUID", referencedColumnName = "JobShippingOrderUUID",insertable = false, updatable = false)
    public List<BoJobcontainer> getBoJobcontainerList() {
        return boJobcontainerList;
    }

 再運行,完全搞定


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM