我需要流式傳輸對象列表,但是當我使用 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
提交
取消