gpt4 book ai didi

java - org.hibernate.LazyInitializationException : failed to lazily initialize a collection of role: studentsystem2. ikubinfo.entity.Student.classes

转载 作者:行者123 更新时间:2023-12-02 09:19:24 32 4
gpt4 key购买 nike

我遇到了这个错误,但我找不到发生的原因。不幸的是,我无法回答其他问题。

SEVERE: Servlet.service() for servlet [rest] in context with path [/studentsystem2] threw exception [Request processing failed; nested exception is org.modelmapper.MappingException: ModelMapper mapping errors:

1) Converter org.modelmapper.internal.converter.CollectionConverter@2f75990d failed to convert java.util.List to java.util.List.

1 error] with root cause
org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: studentsystem2.ikubinfo.entity.Student.classes, could not initialize proxy - no Session

这是我的学生类(class)

Student.java

@Entity
@Table(name="student")
public class Student {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE)
@Column(name="student_id")
private long id;

@NotNull
@Column(name = "firstname")
private String firstName;

@NotNull
@Column(name = "lastname")
private String lastName;

@OneToMany(mappedBy = "student", fetch = FetchType.LAZY)
private List<Classroom> classes;

@NotNull
@Temporal(value = TemporalType.DATE)
private Date birthdate;

private boolean flag;

public Student() {

}

}

我使用 ModelMapper 依赖项从实体转换为模型。下面您可以找到 StudentConverter 类

StudentConverter.java

@Component
public class StudentConverter {

private ModelMapper modelMapper = new ModelMapper();

public StudentConverter() {

}

public Student toEntity(StudentModel model) {
return modelMapper.map(model, Student.class);
}

public StudentModel toModel(Student student) {
return modelMapper.map(student, StudentModel.class);
}

public List<StudentModel> toModel(List<Student> entityList) {
List<StudentModel> modelList = new ArrayList<StudentModel>();
for (Student student : entityList) {
modelList.add(toModel(student));
}
return modelList;
}
}

最佳答案

因为 modelmapper 正在尝试访问(内部使用反射来进行复制)集合 ( private List<Classroom> classes; ) Student 的数据成员事务上下文之外的实体。您必须使用fetch = FetchType.EAGER或者您可以加入以获取此集合以及 Student实体。

已编辑

关于java - org.hibernate.LazyInitializationException : failed to lazily initialize a collection of role: studentsystem2. ikubinfo.entity.Student.classes,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58792941/

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