spring-jpa:PropertyReferenceException:没有找到类型 [PerfectionTask] 的属性 [保存]

Posted

技术标签:

【中文标题】spring-jpa:PropertyReferenceException:没有找到类型 [PerfectionTask] 的属性 [保存]【英文标题】:spring-jpa: PropertyReferenceException: No property [save] found for type [PerfectionTask] 【发布时间】:2020-11-30 20:27:13 【问题描述】:

我们正在尝试将 spring-data-jpa 从 1.10.0 升级到 2.3.2 升级 pom.xml 中的版本后,在尝试部署应用程序(作为 Spring Boot 应用程序)时遇到以下错误

org.springframework.data.mapping.PropertyReferenceException: No property save found for type PerfectionTask!

所以根据我对错误的理解,'save' 方法是 PerfectionTaskRepository 的,但是 spring 试图在 PerfectionTask 实体中找到'save' 方法,它显然不存在。 我只是不确定为什么它试图在实体中找到保存方法

这里是各个存储库的代码 sn-ps:

BaseDefinitionRepository.java

@NoRepositoryBean
public interface BaseDefinitionRepository<T , D extends Serializable> extends JpaRepository<T, D>, JpaSpecificationExecutor<T> 

    <S extends T> S save(S entity, RequestDetails requestDetails);
    <S extends T> S saveAndFlush(S entity, RequestDetails requestDetails);
    <S extends T> List<S> save(Iterable<S> entities, RequestDetails requestDetails);


BaseDefinitionRepositoryImpl.java

@NoRepositoryBean
public class BaseDefinitionRepositoryImpl<T, D extends Serializable>
        extends SimpleJpaRepository<T, D> implements BaseDefinitionRepository<T, D> 

    public BaseDefinitionRepositoryImpl(JpaEntityInformation<T, ?> entityInformation, EntityManager entityManager) 
        super(entityInformation, entityManager);
    

    public BaseDefinitionRepositoryImpl(Class<T> domainClass, EntityManager em) 
        super(domainClass, em);
    

    @Override
    @Transactional
    public <S extends T> S save(S entity, RequestDetails requestDetails) 
        return super.save(entity);
    

    @Override
    @Transactional
    public <S extends T> S saveAndFlush(S entity, RequestDetails requestDetails) 
        return super.saveAndFlush(entity);
    

    @Override
    @Transactional
    public <S extends T>  List<S> save(Iterable<S> entities, RequestDetails requestDetails) 
        return super.saveAll(entities);
    


PerfectionTaskRepository.java

@Repository
public interface PerfectionTaskRepository extends BaseDefinitionRepository<PerfectionTask, Long> 
    
    PerfectionTask findByTmTaskId(String tmTaskId);

    List<PerfectionTask> findByTmTaskIdIn(List<String> tmTaskId);

部署期间错误的整个堆栈跟踪:

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'perfectionTaskRepository' defined in <package-path>.repository.datamodel.PerfectionTaskRepository defined in @EnableJpaRepositories declared on WebAppConfig: Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Failed to create query for method public abstract java.lang.Object <package-path>.repository.definition.BaseDefinitionRepository.saveAndFlush(java.lang.Object,<package-path>.repository.definition.RequestDetails)! No property save found for type PerfectionTask!
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1794) ~[spring-beans-5.2.8.RELEASE.jar:5.2.8.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:594) ~[spring-beans-5.2.8.RELEASE.jar:5.2.8.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:516) ~[spring-beans-5.2.8.RELEASE.jar:5.2.8.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:324) ~[spring-beans-5.2.8.RELEASE.jar:5.2.8.RELEASE]
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:226) ~[spring-beans-5.2.8.RELEASE.jar:5.2.8.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:322) ~[spring-beans-5.2.8.RELEASE.jar:5.2.8.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202) ~[spring-beans-5.2.8.RELEASE.jar:5.2.8.RELEASE]
    at org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:276) ~[spring-beans-5.2.8.RELEASE.jar:5.2.8.RELEASE]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1307) ~[spring-beans-5.2.8.RELEASE.jar:5.2.8.RELEASE]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1227) ~[spring-beans-5.2.8.RELEASE.jar:5.2.8.RELEASE]
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:640) ~[spring-beans-5.2.8.RELEASE.jar:5.2.8.RELEASE]
    ... 59 common frames omitted
Caused by: java.lang.IllegalArgumentException: Failed to create query for method public abstract java.lang.Object <package-path>.repository.definition.BaseDefinitionRepository.saveAndFlush(java.lang.Object,<package-path>.repository.definition.RequestDetails)! No property save found for type PerfectionTask!
    at org.springframework.data.jpa.repository.query.PartTreeJpaQuery.<init>(PartTreeJpaQuery.java:96) ~[spring-data-jpa-2.3.2.RELEASE.jar:2.3.2.RELEASE]
    at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$CreateQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:107) ~[spring-data-jpa-2.3.2.RELEASE.jar:2.3.2.RELEASE]
    at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$CreateIfNotFoundQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:218) ~[spring-data-jpa-2.3.2.RELEASE.jar:2.3.2.RELEASE]
    at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$AbstractQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:81) ~[spring-data-jpa-2.3.2.RELEASE.jar:2.3.2.RELEASE]
    at org.springframework.data.repository.core.support.QueryExecutorMethodInterceptor.lookupQuery(QueryExecutorMethodInterceptor.java:99) ~[spring-data-commons-2.3.2.RELEASE.jar:2.3.2.RELEASE]
    at org.springframework.data.repository.core.support.QueryExecutorMethodInterceptor.lambda$mapMethodsToQuery$1(QueryExecutorMethodInterceptor.java:92) ~[spring-data-commons-2.3.2.RELEASE.jar:2.3.2.RELEASE]
    at java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:193) ~[na:1.8.0_192]
    at java.util.Iterator.forEachRemaining(Iterator.java:116) ~[na:1.8.0_192]
    at java.util.Collections$UnmodifiableCollection$1.forEachRemaining(Collections.java:1049) ~[na:1.8.0_192]
    at java.util.Spliterators$IteratorSpliterator.forEachRemaining(Spliterators.java:1801) ~[na:1.8.0_192]
    at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:481) ~[na:1.8.0_192]
    at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:471) ~[na:1.8.0_192]
    at java.util.stream.ReduceOps$ReduceOp.evaluateSequential(ReduceOps.java:708) ~[na:1.8.0_192]
    at java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234) ~[na:1.8.0_192]
    at java.util.stream.ReferencePipeline.collect(ReferencePipeline.java:499) ~[na:1.8.0_192]
    at org.springframework.data.repository.core.support.QueryExecutorMethodInterceptor.mapMethodsToQuery(QueryExecutorMethodInterceptor.java:94) ~[spring-data-commons-2.3.2.RELEASE.jar:2.3.2.RELEASE]
    at org.springframework.data.repository.core.support.QueryExecutorMethodInterceptor.lambda$new$0(QueryExecutorMethodInterceptor.java:84) ~[spring-data-commons-2.3.2.RELEASE.jar:2.3.2.RELEASE]
    at java.util.Optional.map(Optional.java:215) ~[na:1.8.0_192]
    at org.springframework.data.repository.core.support.QueryExecutorMethodInterceptor.<init>(QueryExecutorMethodInterceptor.java:84) ~[spring-data-commons-2.3.2.RELEASE.jar:2.3.2.RELEASE]
    at org.springframework.data.repository.core.support.RepositoryFactorySupport.getRepository(RepositoryFactorySupport.java:331) ~[spring-data-commons-2.3.2.RELEASE.jar:2.3.2.RELEASE]
    at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.lambda$afterPropertiesSet$5(RepositoryFactoryBeanSupport.java:297) ~[spring-data-commons-2.3.2.RELEASE.jar:2.3.2.RELEASE]
    at org.springframework.data.util.Lazy.getNullable(Lazy.java:212) ~[spring-data-commons-2.3.2.RELEASE.jar:2.3.2.RELEASE]
    at org.springframework.data.util.Lazy.get(Lazy.java:94) ~[spring-data-commons-2.3.2.RELEASE.jar:2.3.2.RELEASE]
    at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.afterPropertiesSet(RepositoryFactoryBeanSupport.java:300) ~[spring-data-commons-2.3.2.RELEASE.jar:2.3.2.RELEASE]
    at org.springframework.data.jpa.repository.support.JpaRepositoryFactoryBean.afterPropertiesSet(JpaRepositoryFactoryBean.java:144) ~[spring-data-jpa-2.3.2.RELEASE.jar:2.3.2.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1853) ~[spring-beans-5.2.8.RELEASE.jar:5.2.8.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1790) ~[spring-beans-5.2.8.RELEASE.jar:5.2.8.RELEASE]
    ... 69 common frames omitted
Caused by: org.springframework.data.mapping.PropertyReferenceException: No property save found for type PerfectionTask!
    at org.springframework.data.mapping.PropertyPath.<init>(PropertyPath.java:94) ~[spring-data-commons-2.3.2.RELEASE.jar:2.3.2.RELEASE]
    at org.springframework.data.mapping.PropertyPath.create(PropertyPath.java:382) ~[spring-data-commons-2.3.2.RELEASE.jar:2.3.2.RELEASE]
    at org.springframework.data.mapping.PropertyPath.create(PropertyPath.java:358) ~[spring-data-commons-2.3.2.RELEASE.jar:2.3.2.RELEASE]
    at org.springframework.data.mapping.PropertyPath.lambda$from$0(PropertyPath.java:311) ~[spring-data-commons-2.3.2.RELEASE.jar:2.3.2.RELEASE]
    at java.util.concurrent.ConcurrentMap.computeIfAbsent(ConcurrentMap.java:324) ~[na:1.8.0_192]
    at org.springframework.data.mapping.PropertyPath.from(PropertyPath.java:293) ~[spring-data-commons-2.3.2.RELEASE.jar:2.3.2.RELEASE]
    at org.springframework.data.mapping.PropertyPath.from(PropertyPath.java:276) ~[spring-data-commons-2.3.2.RELEASE.jar:2.3.2.RELEASE]
    at org.springframework.data.repository.query.parser.Part.<init>(Part.java:82) ~[spring-data-commons-2.3.2.RELEASE.jar:2.3.2.RELEASE]
    at org.springframework.data.repository.query.parser.PartTree$OrPart.lambda$new$0(PartTree.java:251) ~[spring-data-commons-2.3.2.RELEASE.jar:2.3.2.RELEASE]
    at java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:193) ~[na:1.8.0_192]
    at java.util.stream.ReferencePipeline$2$1.accept(ReferencePipeline.java:175) ~[na:1.8.0_192]
    at java.util.Spliterators$ArraySpliterator.forEachRemaining(Spliterators.java:948) ~[na:1.8.0_192]
    at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:481) ~[na:1.8.0_192]
    at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:471) ~[na:1.8.0_192]
    at java.util.stream.ReduceOps$ReduceOp.evaluateSequential(ReduceOps.java:708) ~[na:1.8.0_192]
    at java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234) ~[na:1.8.0_192]
    at java.util.stream.ReferencePipeline.collect(ReferencePipeline.java:499) ~[na:1.8.0_192]
    at org.springframework.data.repository.query.parser.PartTree$OrPart.<init>(PartTree.java:252) ~[spring-data-commons-2.3.2.RELEASE.jar:2.3.2.RELEASE]
    at org.springframework.data.repository.query.parser.PartTree$Predicate.lambda$new$0(PartTree.java:381) ~[spring-data-commons-2.3.2.RELEASE.jar:2.3.2.RELEASE]
    at java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:193) ~[na:1.8.0_192]
    at java.util.stream.ReferencePipeline$2$1.accept(ReferencePipeline.java:175) ~[na:1.8.0_192]
    at java.util.Spliterators$ArraySpliterator.forEachRemaining(Spliterators.java:948) ~[na:1.8.0_192]
    at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:481) ~[na:1.8.0_192]
    at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:471) ~[na:1.8.0_192]
    at java.util.stream.ReduceOps$ReduceOp.evaluateSequential(ReduceOps.java:708) ~[na:1.8.0_192]
    at java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234) ~[na:1.8.0_192]
    at java.util.stream.ReferencePipeline.collect(ReferencePipeline.java:499) ~[na:1.8.0_192]
    at org.springframework.data.repository.query.parser.PartTree$Predicate.<init>(PartTree.java:382) ~[spring-data-commons-2.3.2.RELEASE.jar:2.3.2.RELEASE]
    at org.springframework.data.repository.query.parser.PartTree.<init>(PartTree.java:94) ~[spring-data-commons-2.3.2.RELEASE.jar:2.3.2.RELEASE]
    at org.springframework.data.jpa.repository.query.PartTreeJpaQuery.<init>(PartTreeJpaQuery.java:89) ~[spring-data-jpa-2.3.2.RELEASE.jar:2.3.2.RELEASE]
    ... 95 common frames omitted

【问题讨论】:

您正在从 Spring Boot 2.0.7 升级到 2.3.2 而不是 spring data JPA(具有不同的版本控制方案)。确保 Spring Data JPA 正确检测到您的自定义实现。 嗨@M.Deinum,我的 pom.xml 中 spring-data-jpa 的版本是 2.3.2.RELEASE 是的,它已作为 spring-boot 升级的一部分进行了升级。但不确定如何解决此错误 不,spring-boot-starter-data-jpa 的版本不是 2.3.2(这是一个为您管理 Spring Data JPA 版本的 Spring Boot 启动器!)。如果您使用 Spring Data jpa 的那个版本,那么您的情况会更糟,并且您将旧版本与新内容混合在一起。 而且版本控制方案似乎是正确的 - mvnrepository.com/artifact/org.springframework.data/… 您使用 Spring Boot 的事实应该让您不想管理自己的 Spring Data JPA 版本(在这种情况下也不应该),因为这最终会导致您混合不同版本的 jar(或不兼容的版本)的框架。根据您的标签,您使用的是 Spring Boot,您真的是这样吗? 【参考方案1】:

重命名违规save

我们在方法命名上发生了冲突。我们试图重载“保存”来为单个项目和集合工作。所以我们在存储库和我们的存储库之间有一个接口来定义这两种方法:

public interface BaseRepository<T extends BasePersistable, ID extends Serializable> extends Repository<T, ID> 

    T save(T persisted);

    Iterable<T> save(Iterable<T> persisted);

收藏保存方法出现了异常。所以我们所做的就是将收集方法从save重命名为saveAll,异常就消失了。

public interface BaseRepository<T extends BasePersistable, ID extends Serializable> extends Repository<T, ID> 

    T save(T persisted);

    Iterable<T> saveAll(Iterable<T> persisted);

【讨论】:

【参考方案2】:

如the reference documentation 中所述,您需要通过显式使用@EnableJpaRepositories(repositoryBaseClass = …) 使Spring Data JPA 了解您的基本存储库实现。

否则,SimpleJpaRepository 将被使用,并且您在 BaseDefinitionRepository 上声明的方法将被解释为查询方法,Spring Data 将尝试解析它们,这会导致您看到的异常。

也就是说,我并不完全清楚您为什么首先尝试扩展基类,因为实现并没有真正使用附加参数(还没有?)。我认为这是由于示例的状态,即您试图先启动它但还没有真正完成?

【讨论】:

以上是关于spring-jpa:PropertyReferenceException:没有找到类型 [PerfectionTask] 的属性 [保存]的主要内容,如果未能解决你的问题,请参考以下文章

Spring-JPA 可以与 Postgres 分区一起使用吗?

spring-jpa规格工具

如何添加到列表的链接以使用 spring-jpa 进行排序?

spring-jpa通过自定义sql执行修改碰到的问题

itoo集成jpa

0120 springboot集成Mybatis和代码生成器