我收到了 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/