gpt4 book ai didi

java - Spring boot 无法评估表达式方法抛出 'org.hibernate.LazyInitializationException' 异常。使用 getter,多对多关系

转载 作者:行者123 更新时间:2023-12-04 14:59:46 26 4
gpt4 key购买 nike

我有两个类参会者时间窗口 .
多个参与者可以注册多个 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 集合时。在调试器中,我的 Participant 看起来像这样
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 Repositories 中解决这个问题,但感觉不能使用 getter 是一种浪费。

最佳答案

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

关于java - Spring boot 无法评估表达式方法抛出 'org.hibernate.LazyInitializationException' 异常。使用 getter,多对多关系,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67197737/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com