大佬教程收集整理的这篇文章主要介绍了Spring boot 无法评估表达式方法抛出“org.hibernate.LazyInitializationException”异常。使用 getter,多对多关系,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我有两个班级参与者和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",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 存储库中解决该问题,但感觉无法使用 getter 是一种浪费。
您在关闭事务后尝试使用惰性数据,是的,其中一种方法是使用 EAGER。 另一种方式 - 在使用此数据的方法上使用 @transactional。
以上是大佬教程为你收集整理的Spring boot 无法评估表达式方法抛出“org.hibernate.LazyInitializationException”异常。使用 getter,多对多关系全部内容,希望文章能够帮你解决Spring boot 无法评估表达式方法抛出“org.hibernate.LazyInitializationException”异常。使用 getter,多对多关系所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。