0

我的仓库:</p>

@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 这样的配置,但这似乎没有必要,因为其他存储库很好。

4

2 回答 2

1

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

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

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

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

顺便说一句,你想实现什么?

于 2018-07-28T10:53:32.607 回答
0

它对我来说是这样的:

@NonNull
Page<Product> findAll(@Nullable Specification<Product> specification, @NonNull Pageable pageable);
于 2021-11-16T07:19:55.537 回答