【问题标题】:Clash of methods when implementing Spring JPA实现 Spring JPA 时的方法冲突
【发布时间】:2020-10-14 14:47:52
【问题描述】:

spring-boot-starter-parent 2.3.2.RELEASE

我正在尝试像本教程中那样访问数据库:

https://spring.io/guides/gs/accessing-data-jpa/#_create_simple_queries

这是我的仓库:

package ru.pcask.clients.repositories;
import ru.pcask.clients.entities.Client;
import org.springframework.data.repository.CrudRepository;

public interface ClientRepository extends CrudRepository<Client, Long> {
    Client findById(Long id);
}

还有错误:

'findById(Long)' in 'ru.pcask.clients.repositories.ClientRepository' clashes with 'findById(ID)' in 'org.springframework.data.repository.CrudRepository'; attempting to use incompatible return type

但我收到了屏幕截图中的错误。

【问题讨论】:

  • 去掉那个方法就好了,它已经是CrudRepository的一部分了

标签: spring spring-data-jpa


【解决方案1】:

问题在于您的返回类型。如上所述,findById(Long id) 方法已经在CrudReposistory&lt;T, ID&gt; 中实现,它返回一个Optional&lt;T&gt;

您可以删除代码中的方法,因为它已经存在,或者使用正确的返回类型 Optional&lt;Client&gt; 覆盖它。

【讨论】:

  • 教程错了吗?他们为什么写:客户 findById(long id); ?
  • Optional&lt;T&gt; 数据类型是随 Java 8 引入的。然后,它可能会持续一段时间,直到它被集成到 CrudRepository&lt;T, ID&gt; 中。教程可能是在引入此数据类型之前创建的,并且他们没有更新教程...
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-12-15
  • 2016-03-28
  • 1970-01-01
  • 2023-02-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多