首页 > 解决方案 > Spring boot 无法评估表达式方法抛出 'org.hibernate.LazyInitializationException' 异常。使用 getter,ManyToMany 关系

问题描述

我有两个类ParticipantTimeWindow。多个参与者可以注册多个 TimeWindow,因此是 ManyToMany 关系

@Entity
@Table
public class Participant {
    @Id
    @SequenceGenerator(
            name = "participant_sequence",
            sequenceName = "particant_sequence",
            allocationSize = 1
    )
    @GeneratedValue(
            strategy = GenerationType.SEQUENCE,
            generator = "participant_sequence"
    )
    private Long id;
    private String name;
    private String number;
    private String details;

    @ManyToMany(fetch = FetchType.LAZY)
    @JoinTable(name = "ParticipantCreneaux")
    private Collection<TimeWindow> registeredTimeWindow;


    public Participant() {

    }
    public Participant(String nom, String num, String details) {
        this.name = nom;
        this.number = num;
        this.details = details;
        this.registeredTimeWindow = new ArrayList<>();
    }
    public void addTimeWindow(TimeWindow c){
        registeredTimeWindow.add(c);
    }
    public void removeTimeWindow(TimeWindow c){
        registeredTimeWindow.remove(c);
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getNumber() {
        return number;
    }

    public void setNumber(String number) {
        this.number = number;
    }

    public String getDetails() {
        return details;
    }

    public void setDetails(String details) {
        this.details = details;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public Long getId() {
        return id;
    }

    public Collection<TimeWindow> getRegisteredTimeWindow() {
        return registeredTimeWindow;
    }
}

和 TimeWindow 类:

@Entity
@Table
public class TimeWindow {
    @Id
    @SequenceGenerator(
            name = "creneau_sequence",
            sequenceName = "creneau_sequence",
            allocationSize = 1
    )
    @GeneratedValue(
            strategy = GenerationType.SEQUENCE,
            generator = "creneau_sequence"
    )
    private Long id;
    private LocalTime hourStart;
    private LocalTime hourEnd;

    public Collection<Participant> getListParticipants() {
        return listParticipants;
    }

    @ManyToMany(fetch = FetchType.LAZY,cascade=CascadeType.ALL,mappedBy = "registeredTimeWindow")
    private Collection<Participant> listParticipants;

    public TimeWindow(LocalTime hourStart, LocalTime hourEnd) {
        this.hourStart = hourStart;
        this.hourEnd = hourEnd;
        this.listParticipants = new ArrayList<>();
    }

    public TimeWindow() { }

    public LocalTime getHourEnd() {
        return hourEnd;
    }

    public void setHourStart(LocalTime hourStart) {
        this.hourStart = hourStart;
    }

    public void setHourEnd(LocalTime hourEnd) {
        this.hourEnd = hourEnd;
    }

    public LocalTime getHourStart() {
        return hourStart;
    }

    public int getNbParticipants(){
        return listParticipants.size();
    }
    public void addParticipant(Participant participant){
        this.listParticipants.add(participant);
    }
    public void removeParticipant(Participant participant){
        this.listParticipants.remove(participant);
    }

    public void setId(Long id) {
        this.id = id;
    }

    public Long getId() {
        return id;
    }
}

现在我还在学习 Spring Boot,到目前为止我还没有找到任何关于它的东西或任何对我有帮助的东西。

错误是当我召唤我通过 Config 类中的数据库获得的参与者的 TimeWindow 集合时。在调试器中,我的参与者看起来像这样

id:123
name:"hisName"
number:"321"
details:"some details"
registeredTimeWindow:{PersistentBag@10927}Unable to evaluate the expression Method threw 'org.hibernate.LazyInitializationException' exception.

起初我认为这是正常的,因为 Lazy 选项,我不得不通过 getter 调用数组,但是这是错误的,getter 给了我完全相同的对象。

FetchType.EAGER 工作正常,但我负担不起。我试图从比我更有经验的人那里获得一些帮助,但没有成功。应该可以在 JPA 存储库中解决这个问题,但是感觉不能使用 getter 是一种浪费。

标签: javaspringspring-boothibernatejpa

解决方案


您在关闭事务后尝试使用惰性数据,是的,其中一种方法是使用 EAGER。另一种方式 - 在使用此数据的方法上使用 @Transactional。


推荐阅读