spring - 在 Spring 5 JPA findOne() 中获取 `Long cannot be converted to Example<S>`

标签 spring spring-boot

我收到了 argument mismatch; Long cannot be converted to Example<S>在下面代码中的 findOne 调用上:

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 5 和 Spring 数据 JPA 2.0.0.M3 的一部分,我可以看到 findOne方法在 中被删除CrudRepository 中的一个QueryByExampleExecutor
所以最好改成Optional<T> 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/

关于spring - 在 Spring 5 JPA findOne() 中获取 `Long cannot be converted to Example<S>`,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45582133/

相关文章:

java - 使用自定义范围/角色保护 Spring Boot 应用程序

java - Spring security在提交表单后返回登录页面

java - Spring-MVC : How to ensure multiple users will be able to call a method in sequence

java - 不支持请求方法 'POST' - gradle

java - Spring Boot JPA 基本配置优先于应用程序配置

java - 如何在 Ubuntu 中安装 Spring Boot CLI?

mysql - 在 Hibernate 中启动时创建 mysql 数据库

java - 使用 Spring Boot 拦截请求和响应以获取/添加相关 ID

java - 未找到测试 - 在准系统 Spring Boot Maven 项目上运行 jUnit 5 测试用例时出现空测试套件

java - 在请求 Spring Boot java 中识别作为多部分文件出现的相同图像