S
S
Saintka2021-06-04 09:29:04
Java
Saintka, 2021-06-04 09:29:04

Why does an error occur during lazy initialization when working with a database?

If you remove , then an error will appear in the console, stating that the session is closed, option c is not suitable. Is it possible to somehow get rid of this crutch? I read about but did not understand at what point it should be applied. I am getting an error:@Trasactional(timeout = 2)featch = FeatchType.EAGERHibernate.initialize()

org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: com.SchoolJournal.SpringHibernate.model.ClassRoom.teacher, could not initialize proxy - no Session


I use this test to check the data:
@SpringBootTest 
    @TestInstance(TestInstance.Lifecycle.PER_CLASS) 
    class SpringHibernateApplicationTests { 
    
    @Autowired 
    private PupilRepository pupilRepository; 
    
    @Autowired 
    private ClassRoomRepository classRoomRepository; 
    
    @Autowired 
    private TeacherRepository teacherRepository; 
    
    @BeforeAll 
    @Rollback(value = false) 
    public void contextLoads() { 
    Pupil pupil = new Pupil(); 
    pupil.setName("Anton"); 
    pupil.setSurname("Savridov"); 
    
    Pupil pupil1 = new Pupil(); 
    pupil1.setName("Elena"); 
    pupil1.setSurname("Antonova"); 
    
    Teacher teacher = new Teacher(); 
    teacher.setName("Alla"); 
    teacher.setSurname("Aronova"); 
    teacher.setDiscipline("Mathematics"); 
    
    ClassRoom classRoom = new ClassRoom(); 
    classRoom.setName("1A"); 
    
    pupil.setClassRoom(classRoom); 
    pupil1.setClassRoom(classRoom); 
    teacher.setClassRoom(classRoom); 
    
    classRoomRepository.save(classRoom); 
    teacherRepository.save(teacher); 
    pupilRepository.save(pupil); 
    pupilRepository.save(pupil1); 
    
    } 
    
    @Test 
    @Transaction(timeout = 2)
    public void createEntity() { 
    System.out.println("Класс"); 
    System.out.println("----------------------------"); 
    Iterable<ClassRoom> allClassRoom = classRoomRepository.findAll();
    allClassRoom.forEach(c -> System.out.println(c.getId() + " " + c.getName())); 
    allClassRoom.forEach(c -> c.getTeacher().forEach(t -> System.out.println(t.getId() + " " + t.getName() + " " + t.getSurname() + " " + t.getDiscipline()))); 
    allClassRoom.forEach(c -> c.getPupil().forEach(p -> System.out.println(p.getId() + " " + p.getName() + " " + p.getSurname()))); 
    System.out.println("----------------------------"); 
    }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
O
Orkhan, 2021-06-04
Hasanly @azerphoenix

Good afternoon!
If I'm not mistaken, then you are faced with this problem:
https://www.baeldung.com/spring-open-session-in-view
In simple terms, if you have open-session-in-view enabled, then you do not need to use annotation Transactional, but this is an anti-pattern

Because OSIV creates a Session at the beginning of the request, the transactional proxy uses the current available Session instead of creating a brand new one.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question