【问题标题】:Getting `Long cannot be converted to Example<S>` in Spring 5 JPA findOne()在 Spring 5 JPA findOne() 中获取“Long 无法转换为 Example<S>”
【发布时间】:2018-01-16 20:08:06
【问题描述】:

我在下面的代码中的 findOne 调用中收到argument mismatch; Long cannot be converted to Example&lt;S&gt;

public Optional<AuditEvent> find(Long id) {
    return Optional.ofNullable(persistenceAuditEventRepository.findOne(id))
        .map(auditEventConverter::convertToAuditEvent);
}

以上代码正在转换为 Spring 5 和 Spring Boot 2。它在原始 Spring 4 和 Spring Boot 1 应用程序中运行良好。

任何想法我需要将上面的代码转换成什么?

【问题讨论】:

    标签: spring spring-boot


    【解决方案1】:

    作为 Spring 5 和 Spring data JPA 2.0.0.M3 的一部分,我可以看到 CrudRepository 中的 findOne 方法已删除到 QueryByExampleExecutor 中的方法中 所以最好改成Optional&lt;T&gt; findById(ID arg0);而不是findOne方法 请在下面找到:

    @NoRepositoryBean
    public interface CrudRepository<T, ID> extends Repository<T, ID> {
        <S extends T> S save(S arg0);
    
        <S extends T> Iterable<S> saveAll(Iterable<S> arg0);
    
        Optional<T> findById(ID arg0);
    
        boolean existsById(ID arg0);
    
        Iterable<T> findAll();
    
        Iterable<T> findAllById(Iterable<ID> arg0);
    
        long count();
    
        void deleteById(ID arg0);
    
        void delete(T arg0);
    
        void deleteAll(Iterable<? extends T> arg0);
    
        void deleteAll();
    }
    

    QueryByExampleExecutor

    public abstract interface QueryByExampleExecutor<T> {
        public abstract <S extends T> S findOne(Example<S> paramExample);
    
        public abstract <S extends T> Iterable<S> findAll(Example<S> paramExample);
    
        public abstract <S extends T> Iterable<S> findAll(Example<S> paramExample, Sort paramSort);
    
        public abstract <S extends T> Page<S> findAll(Example<S> paramExample, Pageable paramPageable);
    
        public abstract <S extends T> long count(Example<S> paramExample);
    
        public abstract <S extends T> boolean exists(Example<S> paramExample);
    }
    

    在 QueryForExampleExecutor 上查看文档:

    https://docs.spring.io/spring-data/jpa/docs/2.0.0.RC2/reference/html/

    【讨论】:

      【解决方案2】:

      你也可以使用 getOne() 代替 findOne()

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2019-11-09
        • 2014-09-30
        • 2016-09-03
        • 1970-01-01
        • 2019-12-03
        • 2018-01-19
        • 1970-01-01
        相关资源
        最近更新 更多