在Spring 5 JPA findOne()中获取`Long无法转换为Example< S>` [英] Getting `Long cannot be converted to Example<S>` in Spring 5 JPA findOne()

查看:267
本文介绍了在Spring 5 JPA findOne()中获取`Long无法转换为Example< S>`的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm getting an argument mismatch; Long cannot be converted to Example<S> on the findOne call in the code below:

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

上面的代码将转换为Spring 5和Spring Boot2.在原始的Spring 4和Spring Boot 1应用程序中,它可以正常工作.

The above code is being converted to Spring 5 and Spring Boot 2. It works fine in the original Spring 4 and Spring Boot 1 application.

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

Any ideas what I need to convert the above code to?

推荐答案

作为Spring 5和Spring数据JPA 2.0.0.M3的一部分,我可以看到findOne方法已在 CrudRepository 中删除.到 QueryByExampleExecutor 中的一个 因此最好更改为Optional<T> findById(ID arg0);而不是findOne方法 请在下面找到:

As part of Spring 5 and Spring data JPA 2.0.0.M3 , I could see findOne method is removed in CrudRepository to the one in QueryByExampleExecutor so it is better to change to Optional<T> findById(ID arg0); instead of findOne method Please find below :

@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上检查文档:

Check docs on QueryForExampleExecutor :

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

这篇关于在Spring 5 JPA findOne()中获取`Long无法转换为Example&lt; S&gt;`的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆