简体   繁体   中英

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

I have this error which I cannot find a reason why happens. I cannot relate to the other questions unfortunately.

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

This is my Student 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() {

    }

}

I have used ModelMapper dependency to convert from entity to model. Below you can find the StudentConverter class

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;
    }
}

because modelmapper is trying to access (internally uses reflection for copying purpose) collection ( private List<Classroom> classes; ) data member of Student entity outside of the transaction context. You have to use fetch = FetchType.EAGER or you can join to fetch this collection along with Student entity.

Edited

I had the same problem. First, I added the following code:

"fetch = FetchType.EAGER"

But the problem was not solved.

Then, instead of the above code, I added the following code: "@LazyCollection (LazyCollectionOption.FALSE)"

And the problem was solved.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM