gpt4 book ai didi

spring-data-jpa - JPA - 为什么派生查询返回可选对象?

转载 作者:行者123 更新时间:2023-12-05 02:53:01 25 4
gpt4 key购买 nike

在我的代码中,Appointment 是一个实体,Long id 是它的主键。

在下面的代码中,当我编写 findById(Long id) 时,所需的返回类型是 Optional。但是当我编写 findAppointmentById(Long id) 时,所需的返回类型是 Appointment

findById(Long id)findAppointmentById(Long id) 有什么区别??

public interface AppointmentDao extends CrudRepository<Appointment, Long>{

Optional<Appointment> findById(Long id);

}

最佳答案

What is difference between findById(Long id) and findAppointmentById(Long id) ??

区别是findById(id)已在 CrudRepository 中声明接口(interface)返回可选,并覆盖 Optional 的返回类型与一个无关的Appointment类型在 Java 中是不允许的

But why does findAppointmentById(Long id) return Appointment ? Is it because its a derived query and since i wrote Appointment in the query ,it will return Appoinment?

不是,是因为你声明它返回Appointment .您可以声明它返回 Optional<Appointment>它也会起作用。你可以在 find... 之间放任何你喜欢的东西和 ...By , Spring Data 会简单地忽略它。

关于spring-data-jpa - JPA - 为什么派生查询返回可选对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62394934/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com