Reputation: 188
In my code Appointment
is a entity and Long id
is its primary key.
In the below code when i write findById(Long id)
, the return type required is Optional
.
But when i write findAppointmentById(Long id)
, the return type required is Appointment
.
What is difference between findById(Long id)
and findAppointmentById(Long id)
??
public interface AppointmentDao extends CrudRepository<Appointment, Long>{
Optional<Appointment> findById(Long id);
}
Upvotes: 3
Views: 926
Reputation: 10716
What is difference between findById(Long id) and findAppointmentById(Long id) ??
The difference is findById(id)
is already declared in the CrudRepository
interface as returning an Optional, and overriding a return type of Optional
with an unrelated Appointment
type is not allowed in 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?
No, it's because you declared it to return Appointment
. You can declare it to return Optional<Appointment>
and it will work just as well. You can put anything you like between find...
and ...By
, Spring Data will simply ignore it.
Upvotes: 4