Spring Boot-验证方法公共抽象java.util.stream.Stream的查询失败

时间:2018-10-11 08:36:11

标签: java spring spring-boot 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);
        }

    }

}

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

2 个答案:

答案 0 :(得分:0)

只需删除

@Query(value = "SELECT m FROM MyEntity m")

并使用如下所示的JPARepository的findAll()方法

Stream<MyEntity> findAll();

答案 1 :(得分:0)

您将无法使用body,html{margin:0;padding:0;} #header{width:100%;height:50px;background-color:blue;} #logo{float:left;padding:5px 4px;font-size:200%;width:400px;} #buttonWrapper{background-color:grey;margin:0 auto;width:300px;height:100%; display: -webkit-box; /* OLD - iOS 6-, Safari 3.1-6, BB7 */ display: -ms-flexbox; /* TWEENER - IE 10 */ display: -webkit-flex; /* NEW - Safari 6.1+. iOS 7.1+, BB10 */ display: flex; /* NEW, Spec - Firefox, Chrome, Opera */ justify-content: center; align-items: center;} .tButton{float:left;padding:0 5px;} ,因为Stream<MyEntity> findAll();已经用返回类型JpaRepository定义了该方法,因此您可以像这样List或使用Ordering重命名Stream<MyEntity> getAll();之类的方法。