Springcloud + jpa做两个一对多关联Unable to build Hibernate SessionFactory; nested exception is org.hibernate

Springcloud + jpa 做两个一对多关联时报错:

代码:

//===========================表关联====================================
        //一个product(商品)对应多个deputyPicture(副图)
        @OneToMany(targetEntity = PointsMallDeputyPicture.class, fetch = FetchType.EAGER)
        @JoinColumn(
                name = "product_id", referencedColumnName = "id", insertable = false, updatable = false,
                foreignKey = @ForeignKey(name = "none", value = ConstraintMode.NO_CONSTRAINT)
        )
        @NotFound(action = NotFoundAction.IGNORE)
        private List<PointsMallDeputyPicture> pointsMallDeputyPictureList;

        //一个product(商品)对应多个productSku(规格)
        @OneToMany(targetEntity = PointsMallProductSku.class, fetch = FetchType.EAGER)
        @JoinColumn(
                name = "product_id", referencedColumnName = "id", insertable = false, updatable = false,
                foreignKey = @ForeignKey(name = "none", value = ConstraintMode.NO_CONSTRAINT)
        )
        @NotFound(action = NotFoundAction.IGNORE)
        private List<PointsMallProductSku> pointsMallProductSkuList;

Error:  无法同时获取多个包:[cn.shencom.model.PointsMallProduct.pointsMallDeputyPictureList,cn.shencom.model.PointsMallProduct.pointsMallProductSkuList];

2019-01-03 11:27:18.397  WARN [service-points-mall,,,] 11236 --- [           main] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [cn/shencom/scloud/common/hibernate/MultiTenancyJpaConfiguration.class]: Invocation of init method failed; nested exception is javax.persistence.PersistenceException: [PersistenceUnit: tenantdb-persistence-unit] Unable to build Hibernate SessionFactory; nested exception is org.hibernate.loader.MultipleBagFetchException: cannot simultaneously fetch multiple bags: [cn.shencom.model.PointsMallProduct.pointsMallDeputyPictureList, cn.shencom.model.PointsMallProduct.pointsMallProductSkuList]

解决方法:在看了其他人做的同样的两个一对多关联,却没有报错,原因是 fetch = FetchType.EAGER 只能有一个是立即加载(EAGER);需将另一个该为延迟加载:fetch = FetchType.LAZY

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值