繁体   English   中英

Spring 启动无法评估表达式方法抛出“org.hibernate.LazyInitializationException”异常。 使用 getter,ManyToMany 关系

[英]Spring boot Unable to evaluate the expression Method threw 'org.hibernate.LazyInitializationException' exception. using getter, ManyToMany relation

我有两个类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;
    }
}

和时间窗口 class:

@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 引导,到目前为止我还没有找到任何关于它的东西或任何对我有帮助的东西。

错误是当我召唤我通过配置 class 中的数据库获得的参与者的 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 给了我完全相同的 object。

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

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

我收到了这个错误:

方法抛出“org.hibernate.LazyInitializationException”异常。

这是因为目前不存在 session。 Hibernate 打开 session 并关闭它,但对于“lazy = true”或“fetch = FetchType.LAZY”,此类字段由代理填充。 当您尝试查找此类字段的值时,它将尝试使用 go 到数据库中使用活动的 session 来检索数据。 如果找不到这样的 session,你会得到这个异常。

您可以使用“lazy=false”修复它或检查您是否正确使用了@Transcational(尝试在您的服务层而不是数据访问层中使用它),您也可以使用

@Transactional(传播 = Propagation.REQUIRED,rollbackFor = Exception.class)

或者

@Transactional

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM