gpt4 book ai didi

java - @Transactional 上的 Spring Hibernate LazyInitializationException

转载 作者:太空宇宙 更新时间:2023-11-04 12:09:12 25 4
gpt4 key购买 nike

我有一个在启动时运行异步任务并递归运行的服务:

@Service
public class TaskService {

private final TaskRepository taskRepository;

@Inject
public TaskService(TaskRepository taskRepository) {
this.taskRepository= taskRepository;
}

private final int currentTaskId = -1;

@Transactional
@PostConstruct
private void init() {
taskRepository.findByClosedDateIsNull().forEach(taskRepository::delete);
runTask();
}

@Async
@Transactional
private void runTask() {
if (!getCurrent().isPresent()) {
Task task = new Task();
//set props
currentTaskId = taskRepository.save(task).getId();
}
Util.sleep(5000); //wrapper for simple Thread.sleep(long l).
Task task = getCurrent().get();
if (task.getEvents().size > 0) {
//bussiness logic
Util.sleep(1000);
}
runTask();
}

@Transactional(readOnly = true)
private Optional<Task> getCurrent() {
return taskRepository.findOneById(currentTaskId).map(task -> {
task.getEvents().size(); //throws the error here
return task;
});
}

}

堆栈跟踪:

Caused by: org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: com.test.domain.Task.events, could not initialize proxy - no Session at org.hibernate.collection.internal.AbstractPersistentCollection.throwLazyInitializationException(AbstractPersistentCollection.java:576) at org.hibernate.collection.internal.AbstractPersistentCollection.withTemporarySessionIfNeeded(AbstractPersistentCollection.java:215) at org.hibernate.collection.internal.AbstractPersistentCollection.readSize(AbstractPersistentCollection.java:156) at org.hibernate.collection.internal.PersistentSet.size(PersistentSet.java:160) at com.test.service.TaskService.lambda$getCurrent$5(TaskService.java:135) at java.util.Optional.map(Optional.java:215) at com.test.service.TaskService.getCurrent(TaskService.java:134) at com.test.service.TaskService.runTask(TaskService.java:163) at com.test.service.TaskService.init(TaskService.java:66) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor$LifecycleElement.invoke(InitDestroyAnnotationBeanPostProcessor.java:365) at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor$LifecycleMetadata.invokeInitMethods(InitDestroyAnnotationBeanPostProcessor.java:310) at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor.postProcessBeforeInitialization(InitDestroyAnnotationBeanPostProcessor.java:133) ... 21 common frames omitted

我还尝试了 Hibernate.initialize(Object proxy);。 OpenViewSessionFilter 不是我的解决方案。我不想将此集合设置为 EAGER。

最佳答案

我找到了一个解决方案(不知道这是否是一个不好的做法,但它对我有用)。只需在 JpaRepository 中创建一个默认方法并使用 @Transactional 对其进行注释即可。在服务中调用此方法。

关于java - @Transactional 上的 Spring Hibernate LazyInitializationException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40031106/

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