Spring Data Cassandra:“用户类型没有属性 findAll”

Posted

技术标签:

【中文标题】Spring Data Cassandra:“用户类型没有属性 findAll”【英文标题】:Spring Data Cassandra: "No property findAll for type User" 【发布时间】:2017-12-18 16:18:57 【问题描述】:

我有一个非常简单的 spring data cassandra 项目,其应用程序上下文无法启动。整个项目由四个文件组成:域对象、存储库接口、cassandra 配置和应用程序入口点。这是域对象:

@Table
public class User 
    @PrimaryKeyColumn(name = "user_id", ordinal = 0, type = PrimaryKeyType.PARTITIONED)
    public UUID userId;

和存储库接口:

@Repository
public interface UserRepository extends PagingAndSortingRepository<User, UUID> 

配置很简单:

@Configuration
public class TestSpringDataCassandraConfiguration extends AbstractCassandraConfiguration 
    @Override 
    protected String getKeyspaceName() 
        return "company";
    
    @Override 
    protected List<CreateKeyspaceSpecification> getKeyspaceCreations() 
        return Collections.singletonList(CreateKeyspaceSpecification
                .createKeyspace(getKeyspaceName())
                .ifNotExists()
                .withSimpleReplication(1));
    
    @Override 
    public String[] getEntityBasePackages() 
        return new String[]  "com.company.project.cassandra.entity";
    
    @Override 
    public SchemaAction getSchemaAction() 
        return SchemaAction.CREATE_IF_NOT_EXISTS;
    

并且入口点尽可能简单。

@SpringBootApplication
public class TestApplication 
    public static void main(String[] sa) 
        SpringApplication.run(TestApplication.class, sa);
    

以下是项目依赖项:

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter</artifactId>
        <version>1.5.4.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-cassandra</artifactId>
        <version>1.5.4.RELEASE</version>
    </dependency>
</dependencies>

尽管如此,应用程序仍无法启动并出现以下错误:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userRepository': Invocation of init method failed; nested exception is org.springframework.data.mapping.PropertyReferenceException: No property findAll found for type User!
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1628) ~[spring-beans-4.3.9.RELEASE.jar:4.3.9.RELEASE]
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:555) ~[spring-beans-4.3.9.RELEASE.jar:4.3.9.RELEASE]
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483) ~[spring-beans-4.3.9.RELEASE.jar:4.3.9.RELEASE]
        at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306) ~[spring-beans-4.3.9.RELEASE.jar:4.3.9.RELEASE]
        at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) ~[spring-beans-4.3.9.RELEASE.jar:4.3.9.RELEASE]
        at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302) ~[spring-beans-4.3.9.RELEASE.jar:4.3.9.RELEASE]
        at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197) ~[spring-beans-4.3.9.RELEASE.jar:4.3.9.RELEASE]
        at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:742) ~[spring-beans-4.3.9.RELEASE.jar:4.3.9.RELEASE]
        at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:867) ~[spring-context-4.3.9.RELEASE.jar:4.3.9.RELEASE]
        at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:543) ~[spring-context-4.3.9.RELEASE.jar:4.3.9.RELEASE]
        at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:693) [spring-boot-1.5.4.RELEASE.jar:1.5.4.RELEASE]
        at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:360) [spring-boot-1.5.4.RELEASE.jar:1.5.4.RELEASE]
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:303) [spring-boot-1.5.4.RELEASE.jar:1.5.4.RELEASE]
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:1118) [spring-boot-1.5.4.RELEASE.jar:1.5.4.RELEASE]
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:1107) [spring-boot-1.5.4.RELEASE.jar:1.5.4.RELEASE]
        at com.company.project.Application.main(Application.java:9) [classes/:na]
Caused by: org.springframework.data.mapping.PropertyReferenceException: No property findAll found for type User!
        at org.springframework.data.mapping.PropertyPath.<init>(PropertyPath.java:77) ~[spring-data-commons-1.13.4.RELEASE.jar:na]
        at org.springframework.data.mapping.PropertyPath.create(PropertyPath.java:329) ~[spring-data-commons-1.13.4.RELEASE.jar:na]
        at org.springframework.data.mapping.PropertyPath.create(PropertyPath.java:309) ~[spring-data-commons-1.13.4.RELEASE.jar:na]
        at org.springframework.data.mapping.PropertyPath.from(PropertyPath.java:272) ~[spring-data-commons-1.13.4.RELEASE.jar:na]
        at org.springframework.data.mapping.PropertyPath.from(PropertyPath.java:243) ~[spring-data-commons-1.13.4.RELEASE.jar:na]
        at org.springframework.data.repository.query.parser.Part.<init>(Part.java:76) ~[spring-data-commons-1.13.4.RELEASE.jar:na]
        at org.springframework.data.repository.query.parser.PartTree$OrPart.<init>(PartTree.java:247) ~[spring-data-commons-1.13.4.RELEASE.jar:na]
        at org.springframework.data.repository.query.parser.PartTree$Predicate.buildTree(PartTree.java:398) ~[spring-data-commons-1.13.4.RELEASE.jar:na]
        at org.springframework.data.repository.query.parser.PartTree$Predicate.<init>(PartTree.java:378) ~[spring-data-commons-1.13.4.RELEASE.jar:na]
        at org.springframework.data.repository.query.parser.PartTree.<init>(PartTree.java:86) ~[spring-data-commons-1.13.4.RELEASE.jar:na]
        at org.springframework.data.cassandra.repository.query.PartTreeCassandraQuery.<init>(PartTreeCassandraQuery.java:47) ~[spring-data-cassandra-1.5.4.RELEASE.jar:na]
        at org.springframework.data.cassandra.repository.support.CassandraRepositoryFactory$CassandraQueryLookupStrategy.resolveQuery(CassandraRepositoryFactory.java:163) ~[spring-data-cassandra-1.5.4.RELEASE.jar:na]
        at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.<init>(RepositoryFactorySupport.java:436) ~[spring-data-commons-1.13.4.RELEASE.jar:na]
        at org.springframework.data.repository.core.support.RepositoryFactorySupport.getRepository(RepositoryFactorySupport.java:221) ~[spring-data-commons-1.13.4.RELEASE.jar:na]
        at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.initAndReturn(RepositoryFactoryBeanSupport.java:277) ~[spring-data-commons-1.13.4.RELEASE.jar:na]
        at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.afterPropertiesSet(RepositoryFactoryBeanSupport.java:263) ~[spring-data-commons-1.13.4.RELEASE.jar:na]
        at org.springframework.data.cassandra.repository.support.CassandraRepositoryFactoryBean.afterPropertiesSet(CassandraRepositoryFactoryBean.java:76) ~[spring-data-cassandra-1.5.4.RELEASE.jar:na]
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1687) ~[spring-beans-4.3.9.RELEASE.jar:4.3.9.RELEASE]
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1624) ~[spring-beans-4.3.9.RELEASE.jar:4.3.9.RELEASE]
        ... 15 common frames omitted

真正奇怪的是,如果UserRepository 扩展了CrudRepository,应用程序就可以正常启动。有人知道这个设置有什么问题吗?

【问题讨论】:

Spring Data for Apache Cassandra 不支持PagingAndSortingRepository。只使用CrudRepository 什么?是的,它确实。 docs.spring.io/spring-data/cassandra/docs/1.5.4.RELEASE/… 您指的是通用文档部分,解释了存储库的一般工作方式 (This chapter explains the core concepts and interfaces of Spring Data repositories. The information in this chapter is pulled from the Spring Data Commons module. It uses the configuration and code samples for the Java Persistence API (JPA) module.)。 Welp,这一点也不令人困惑。文档中没有任何地方说不能使用分页。你知道是否支持计数方法吗?我也有这些问题。继续添加答案;谢谢你的帮助。 通过 count() 支持计数,但不支持计数投影 (countByFirstnameandLastname(…))。想就您的计数问题发布一个新问题吗? 【参考方案1】:

将@mp911de 的评论变成正确答案:

Apache Cassandra 的 Spring Data 不支持 PagingAndSortingRepository。只使用CrudRepository

不幸的是,参考文档提到了支持PagingAndSortingRepository 的功能,但这只是关于 Spring Data 如何工作的一般部分。

有计划改进文档:https://jira.spring.io/browse/DATACMNS-1086

【讨论】:

在官方文档中添加注释:Cassandra repositories do not extend PagingAndSortingRepository, because classic paging patterns using limit/offset are not applicable to Cassandra.docs.spring.io/spring-data/cassandra/docs/current/reference/…

以上是关于Spring Data Cassandra:“用户类型没有属性 findAll”的主要内容,如果未能解决你的问题,请参考以下文章

Spring Boot 1.4:Spring Data Cassandra 1.4.2 与 Cassandra 3.0 不兼容?

具有响应式Cassandra 的Spring Data

Spring Data Cassandra 计数器更新

Spring Boot 1.5.x with Spring Data Cassandra 1.5.x 与 Cassandra 2.1 不兼容

使用 Spring-data-cassandra 查询具有复合主键的表

使用 spring-boot-data 在 Cassandra 中进行分页