Я использую Hibernate 6 с Spring 6 и столкнулся с проблемой, связанной со следующим исключением:
org.hibernate.LazyInitializationException: failed to lazily initialize a collection
Это происходит несмотря на то, что в моем отношении @OneToMany установлена стратегия FetchType.EAGER.
Вот моя сущность код для справки:
@Entity
public class Parent {
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
private Set children;
// Getters and setters
}
Мой сервис
@Service
public class ParentService {
@Autowired
private ParentRepository parentRepository;
// Service method with @Transactional to ensure the session is active during fetching
@Transactional
public Parent getParentWithChildren(Long parentId) {
// Fetch the parent by its ID
Parent parent = parentRepository.findById(parentId)
.orElseThrow(() -> new RuntimeException("Parent not found"));
// At this point, parent.getChildren() should already be eagerly loaded (since FetchType.EAGER is used).
Set children = parent.getChildren(); // This should not throw
azyInitializationException (!!!)
return parent;
}
}
Подробнее здесь: https://stackoverflow.com/questions/792 ... initialize
Я получаю org.hibernate.LazyInitializationException: не удалось лениво инициализировать коллекцию при использовании Fetc ⇐ JAVA
-
- Похожие темы
- Ответы
- Просмотры
- Последнее сообщение