我有此错误,我找不到发生此错误的原因。不幸的是,我无法解决其他问题。
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
这是我的学生班
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;
}
}
答案 0 :(得分:0)
因为modelmapper试图在事务上下文外部访问private List<Classroom> classes;
实体的数据成员(内部使用反射进行复制)(Student
)。
您必须使用fetch = FetchType.EAGER
,也可以加入以与Student
实体一起获取此集合。
已编辑
答案 1 :(得分:0)
我有同样的问题。首先,我添加了以下代码:
“获取= FetchType.EAGER”
但是问题没有解决。
然后,我添加了以下代码,而不是上面的代码: “ @LazyCollection(LazyCollectionOption.FALSE)”
问题解决了。