findById() 和 find<classname>ById() 有什么区别?

What is the difference between findById() and find<classname>ById()?

在我的 spring 数据项目中,有一个如下所示的实体:

public class Employee {

    @Id
    @GeneratedValue(strategy= GenerationType.IDENTITY)
    @Column(name="id")
    private Integer id;

    @Column(name="category")
    private Category category;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name="element_id")
    private Department department;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name="agency_id")
    private Agency agency;

    public Employee() {}

    // routine getters and setters
}

在我的 EmployeeRepository 中,我发现我可以派生一个方法 findEmployeeById(),它的工作方式似乎与通常的 findById() 完全相同(但我不确定)。有人可以解释这两种方法之间的区别吗?

Optional<Employee> findEmployeeById (Integer id);

Optional<Employee> findById (Integer id);

我通过我这边的自动完成 (IntelliJ) 偶然“发现”了这个。

不同之处在于,findEmployeeById() 按其字段搜索员工 named idfindById 按字段搜索注释@Id,忽略实体 ID 字段的名称。

在你的情况下 - 与许多其他人一样 - 问题是 @Id 字段恰好被命名为 id 所以结果是相同的。