jpa LazyInitializationException:尽管使用了@ transmitting,但仍无法延迟初始化角色集合

gcuhipw9  于 2023-10-19  发布在  其他
关注(0)|答案(1)|浏览(125)

我声明了@ transmitting,但仍然得到错误:无法懒惰地初始化一个角色集合:fa.training.model.KhachHang.suDungMay,无法初始化代理-无会话。
具体而言:在AllCustomerInfoController类中,我希望获得使用SuDungMay和SuDungDichVu的所有客户的列表:

@Controller
public class AllCustomerInfoController {

    @Autowired
    private KhachHangRepository khachHangRepository;

    @GetMapping(value = "/")
    @Transactional
    public String allCustomerInfo(Model model) {
        List<KhachHang> listKhachHang = khachHangRepository.findAll();
        model.addAttribute("listAllCustomer", listKhachHang);
        return "allcustomerinfo";
    }
}

我的实体类:

@Entity
public class KhachHang {

    @Id
    private String maKh;
    private String tenKh;
    private String diaChi;
    private String sdt;
    private String email;
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "khachHang")
    private List<SuDungMay> suDungMay;
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "khachHang")
    private List<SuDungDichVu> suDungDichVu;
}

@Entity
public class SuDungDichVu {
    @Valid
    @EmbeddedId
    private SuDungDichVuId suDungDichVuId;
    private int soLuong;
    @ManyToOne
    @JoinColumn(name = "maKh")
    @MapsId(value = "maKh")
    private KhachHang khachHang;
    @ManyToOne
    @JoinColumn(name = "maDv")
    @MapsId(value = "maDv")
    private DichVu dichVu;
}

@Entity
public class SuDungMay {
    @EmbeddedId
    @Valid
    private SuDungMayId suDungMayId;
    private double thoiGianSd;
    @ManyToOne
    @JoinColumn(name = "maKh")
    @MapsId(value = "maKh")
    private KhachHang khachHang;
    @ManyToOne
    @JoinColumn(name = "maMay")
    @MapsId(value = "maMay")
    private May may;
}

如何获得客户名单包含列表SuDungMay和SuDungDichVu没有懒惰的错误,谢谢!`

u7up0aaq

u7up0aaq1#

当然,它甚至不能与@ transmitted一起工作,因为你所做的只是将实体的引用添加到模型中并返回。当构造View时,事务被关闭,对象被访问。在您的情况下,您需要急切地获取或Map到allCustomerInfo内部的Dto,这种方式应该可以工作。

相关问题