【问题标题】:JPA No property findAll found for type ProductJPA 没有找到类型 Product 的属性 findAll
【发布时间】:2019-01-05 06:19:03
【问题描述】:

我的仓库:

@Repository
public interface ProductDao extends JpaRepository<Product,Long>,JpaSpecificationExecutor<Product> {

   Page<Product> findAll(Specification<Product> specification,Pageable pageable);
}

我的服务:

    ...  
@Autowired
        public ProductServiceImpl(ProductRepository repository, ProductDao productDao, ProductImgService productImgService, ProductClassService productClassService, AuthUserService authUserService, DeviceService deviceService){
        super(repository);
            this.productDao = productDao;
            this.productImgService = productImgService;
            this.productClassService = productClassService;
            this.authUserService = authUserService;
            this.deviceService = deviceService;
        }

        final
        ProductDao productDao; 
...

我在服务中这样使用它:

midResult = productDao.findAll((root, criteriaQuery, criteriaBuilder) -> {
            List<Predicate> list = new ArrayList<>();
            list.add(criteriaBuilder.equal(root.get("ouId").as(Long.class),product.getOuId()));
            if(product.getPclassId()!=null)
                list.add(criteriaBuilder.equal(root.get("pclassId").as(Long.class),product.getPclassId()));
            if(product.getProductStatus()!=null)
                list.add(criteriaBuilder.equal(root.get("productStatus").as(Integer.class),product.getProductStatus()));
            if(product.getProductName()!=null)
                list.add(criteriaBuilder.equal(root.get("productName").as(String.class),product.getProductName()));
            if(product.getCreator()!=null)
                list.add(criteriaBuilder.equal(root.get("creator").as(Integer.class),product.getCreator()));
            list.add(criteriaBuilder.between(root.get("createTimeTmp").as(Long.class),Long.parseLong(finalStarttsp),Long.parseLong(finalEndtsp)));
            Predicate[] p = new Predicate[list.size()];
            criteriaQuery.where(criteriaBuilder.and(list.toArray(p)));
            return criteriaQuery.getRestriction();
        }, pageable);

产品类定义很好。

当我运行这段代码时,错误竟然是:

Caused by: org.springframework.data.mapping.PropertyReferenceException: No property findAll found for type Product!
    at org.springframework.data.mapping.PropertyPath.<init>(PropertyPath.java:77) ~[spring-data-commons-1.13.6.RELEASE.jar:na]
    at org.springframework.data.mapping.PropertyPath.create(PropertyPath.java:329) ~[spring-data-commons-1.13.6.RELEASE.jar:na]
    at org.springframework.data.mapping.PropertyPath.create(PropertyPath.java:309) ~[spring-data-commons-1.13.6.RELEASE.jar:na]
    at org.springframework.data.mapping.PropertyPath.from(PropertyPath.java:272) ~[spring-data-commons-1.13.6.RELEASE.jar:na]
    at org.springframework.data.mapping.PropertyPath.from(PropertyPath.java:243) ~[spring-data-commons-1.13.6.RELEASE.jar:na]
    at org.springframework.data.repository.query.parser.Part.<init>(Part.java:76) ~[spring-data-commons-1.13.6.RELEASE.jar:na]
    at org.springframework.data.repository.query.parser.PartTree$OrPart.<init>(PartTree.java:247) ~[spring-data-commons-1.13.6.RELEASE.jar:na]
    at org.springframework.data.repository.query.parser.PartTree$Predicate.buildTree(PartTree.java:398) ~[spring-data-commons-1.13.6.RELEASE.jar:na]
    at org.springframework.data.repository.query.parser.PartTree$Predicate.<init>(PartTree.java:378) ~[spring-data-commons-1.13.6.RELEASE.jar:na]
    at org.springframework.data.repository.query.parser.PartTree.<init>(PartTree.java:86) ~[spring-data-commons-1.13.6.RELEASE.jar:na]
    at org.springframework.data.mybatis.repository.query.PartTreeMybatisQuery.<init>(PartTreeMybatisQuery.java:81) ~[spring-data-mybatis-1.0.17.RELEASE.jar:na]
    at org.springframework.data.mybatis.repository.query.MybatisQueryLookupStrategy$CreateQueryLookupStrategy.resolveQuery(MybatisQueryLookupStrategy.java:71) ~[spring-data-mybatis-1.0.17.RELEASE.jar:na]
    at org.springframework.data.mybatis.repository.query.MybatisQueryLookupStrategy$CreateIfNotFoundQueryLookupStrategy.resolveQuery(MybatisQueryLookupStrategy.java:120) ~[spring-data-mybatis-1.0.17.RELEASE.jar:na]
    at org.springframework.data.mybatis.repository.query.MybatisQueryLookupStrategy$AbstractQueryLookupStrategy.resolveQuery(MybatisQueryLookupStrategy.java:50) ~[spring-data-mybatis-1.0.17.RELEASE.jar:na]
    at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.<init>(RepositoryFactorySupport.java:436) ~[spring-data-commons-1.13.6.RELEASE.jar:na]
    at org.springframework.data.repository.core.support.RepositoryFactorySupport.getRepository(RepositoryFactorySupport.java:221) ~[spring-data-commons-1.13.6.RELEASE.jar:na]
    at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.initAndReturn(RepositoryFactoryBeanSupport.java:277) ~[spring-data-commons-1.13.6.RELEASE.jar:na]
    at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.afterPropertiesSet(RepositoryFactoryBeanSupport.java:263) ~[spring-data-commons-1.13.6.RELEASE.jar:na]
    at org.springframework.data.mybatis.repository.support.MybatisRepositoryFactoryBean.afterPropertiesSet(MybatisRepositoryFactoryBean.java:64) ~[spring-data-mybatis-1.0.17.RELEASE.jar:na]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1687) ~[spring-beans-4.3.10.RELEASE.jar:4.3.10.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1624) ~[spring-beans-4.3.10.RELEASE.jar:4.3.10.RELEASE]

我正在使用 springboot 并且没有像 @EnableJpaRepositories 这样的配置 但这似乎没有必要,因为其他存储库很好。

【问题讨论】:

    标签: spring spring-boot spring-data-jpa spring-data jpa-criteria


    【解决方案1】:

    您以不正确的方式使用 spring-data。当您在存储库中编写方法 findAll 时,它将在实体中查找给定的属性。

    例如,您有一个具有 firstName 属性的实体 User。然后你可以定义方法,如 findByFirstName

    在给定的示例中,spring-data 查找属性 all 不能作为 Product 的一部分使用。

    在这种情况下,您需要使用 @Query 注释编写自己的查询。

    顺便说一句,你想达到什么目的?

    【讨论】:

    • 感谢您的回答,但是当我删除存储库中的 findAll() 方法时,它仍然是相同的错误。我正在尝试使用 Page findAll(Specification spec, Pageable pageable);在接口 JpaSpecificationExecutor 中,你能教我如何使用它吗?还是举个例子?
    【解决方案2】:

    对我来说是这样的:

    @NonNull
    Page<Product> findAll(@Nullable Specification<Product> specification, @NonNull Pageable pageable);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-10-16
      • 2020-09-01
      • 2022-01-18
      • 2019-04-08
      • 2020-08-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多