JPA没有找到属性找到所有类型的产品

时间:2018-07-28 08:33:45

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

我的资料库:

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

1 个答案:

答案 0 :(得分:0)

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

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

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

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

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