Java 如何使spring data@Async工作?错误:PersistentEntity不能为null
当我尝试在Spring数据存储库方法上使用@Async时,出现以下错误: {“原因”:null,“消息”:“persistentenentity不能为null!”} 好像它试图序列化CompletableFuture 存储库代码:Java 如何使spring data@Async工作?错误:PersistentEntity不能为null,java,spring-data,Java,Spring Data,当我尝试在Spring数据存储库方法上使用@Async时,出现以下错误: {“原因”:null,“消息”:“persistentenentity不能为null!”} 好像它试图序列化CompletableFuture 存储库代码: @Query("SELECT p FROM Produto p where descricao LIKE CONCAT(UPPER(:like),'%')") @Async CompletableFuture<List<Produto>> fi
@Query("SELECT p FROM Produto p where descricao LIKE CONCAT(UPPER(:like),'%')")
@Async
CompletableFuture<List<Produto>> findByLikeAsync(@Param("like") String like);
我尝试使用Future而不是CompletableFuture,但它抛出了相同的错误
有什么想法吗
谢谢。假设您使用的是
crudepository
,存储库中的方法只能返回它们注册的类(或它们的集合)。在这种情况下,如果已将存储库定义为:
公共接口ProdutRepository扩展了Crudepository
您必须返回
列表
。您可以创建一个调用此函数的@Service
,并在其中使用@Async
注释一个公共方法。为什么“持久实体不能为null”会让您认为它正在尝试序列化一个CompletableFuture
?stacktrace应该告诉您真正的错误在哪里。我直接调用异步方法。我一定是做错了什么。我刚开始学习spring,也许我误解了什么。
@SpringBootApplication
@EntityScan(basePackages = {"..."})
@EnableJpaRepositories(basePackages = {"..."})
@EnableAsync
@Configuration
public class Application implements AsyncConfigurer {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
@Override
public Executor getAsyncExecutor() {
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
executor.setCorePoolSize(7);
executor.setMaxPoolSize(42);
executor.setQueueCapacity(11);
executor.setThreadNamePrefix("MyExecutor-");
executor.initialize();
return executor;
}
@Override
public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() {
return new CustomAsyncExceptionHandler();
}
}