Spring Boot - 查询方法公共抽象 java.util.stream.Stream

我需要流式传输对象列表,但是当我使用 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 代码:


@Repository

public 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

@EnableAutoConfiguration

public 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);

        }


    }


}

我已经尝试了一切,但没有任何效果,请帮助我!


墨色风雨
浏览 153回答 2
2回答

森林海

您将无法使用,Stream<MyEntity> findAll();因为JpaRepository已经定义了具有List返回类型的方法,因此您可以像这样重命名方法Stream<MyEntity> getAll();或使用 Ordering 将方法重命名为Stream<MyEntity> findAllByOrderByIdAsc();.

拉丁的传说

只需删除@Query(value&nbsp;=&nbsp;"SELECT&nbsp;m&nbsp;FROM&nbsp;MyEntity&nbsp;m")并使用 JPARepository 的 findAll() 方法,如下所示Stream<MyEntity>&nbsp;findAll();
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java