我需要流式传输对象列表,但是当我使用 JpaRepository 和 @Query 尝试它时,我收到此异常:Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'MainApplication': Unsatisfied dependency expressed through field 'service'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'MyServiceImpl': Unsatisfied dependency expressed through field 'myDAO'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'MyDAO': Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Validation failed for query for method public abstract java.util.stream.Stream com.my.package.dao.MyDAO.streamAll()!MyDAO 代码:@Repositorypublic interface MyDAO extends JpaRepository<MyEntity, Long> { @QueryHints(value = @QueryHint(name = HINT_FETCH_SIZE, value = "" + Integer.MIN_VALUE)) @Query(value = "SELECT m FROM MyEntity m") Stream<MyEntity> streamAll(); ...}主要代码:@SpringBootApplication@ComponentScan("com.my.package.*")@EntityScan("com.my.package.*")@Configuration@EnableAutoConfigurationpublic class MainApplication implements CommandLineRunner { @Autowired MyServiceInterface service; public static void main(String[] args) { SpringApplication.run(MainApplication.class, args); } @Override public void run(String... args) throws Exception { try { service.createCsv(); } catch (RuntimeException e) { System.out.println(e); } }}我已经尝试了一切,但没有任何效果,请帮助我!
2 回答
森林海
TA贡献2011条经验 获得超2个赞
您将无法使用,Stream<MyEntity> findAll();
因为JpaRepository
已经定义了具有List
返回类型的方法,因此您可以像这样重命名方法Stream<MyEntity> getAll();
或使用 Ordering 将方法重命名为Stream<MyEntity> findAllByOrderByIdAsc();
.
拉丁的传说
TA贡献1789条经验 获得超8个赞
只需删除
@Query(value = "SELECT m FROM MyEntity m")
并使用 JPARepository 的 findAll() 方法,如下所示
Stream<MyEntity> findAll();
添加回答
举报
0/150
提交
取消