java - Spring boot 无法评估表达式方法抛出 'org.hibernate.LazyInitializationException' 异常。使用 getter,ManyToMany 关系
问题描述
我有两个类Participant和TimeWindow。多个参与者可以注册多个 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 是一种浪费。
解决方案
您在关闭事务后尝试使用惰性数据,是的,其中一种方法是使用 EAGER。另一种方式 - 在使用此数据的方法上使用 @Transactional。
推荐阅读
- java - Mallet 输出主题权重 0.0 或 1.0,两者之间没有
- android - pagerView 中的替换片段
- php - 查询在表中添加两个图像不起作用
- typescript - 我可以将数字变量与“类验证器”进行比较吗?
- postgresql - 如何检查是否使用 MATCH FULL 或 MATCH SIMPLE 约束创建了引用多个列的外键?
- python - 我有一个“开始日期”和“结束日期”列。我想把每一行变成 X 行,每个包含日期
- node.js - 如何使用 edit-json-file npm 读取和更新数组中的值
- jquery - 如何在jQuery.qrcode中动态设置文本的值
- ruby-on-rails - 用户能够看到所有者属性 Rails 5.2
- php - 如何从 Laravel 中的当前时间减去时间?