Issue
I am trying to fetch all rows with my foreign key attribute but I am getting error
Doctor class
@Entity
@Table(name = "doctors")
public class Doctor implements Serializable {
@Column(name = "doctor_id")
private @Id @GeneratedValue(strategy = GenerationType.IDENTITY) int doctorid;
private @NotBlank String doctor_name;
private @NotBlank String speciality;
Appointment class
@Entity
@Table(name = "appointments")
public class Appointment implements Serializable {
@Id
@GeneratedValue
private @NotBlank int App_id;
private @NotBlank String date;
private @NotBlank String time;
@ManyToOne(targetEntity = Doctor.class,cascade = {CascadeType.ALL})
@JsonBackReference("doctors")
@JoinColumn(name = "doctor_id")
private Doctor doctor_id;
Controller
@GetMapping("/appointmentsbydoctor")
public ResponseEntity<Object> doctorsappointment(@Valid @RequestParam(name = "doctor_id") int id) {
return new ResponseEntity<>(appointmentService.appointment(id), HttpStatus.OK);
}
Service class
public List<Appointment> appointment(int id){
List<Appointment> a= appointmentRepository.findAllByDoc(id);
return a;
}
Repository
public interface AppointmentRepository extends JpaRepository<Appointment, Integer> {
@Query(name="select * from Appointment a where a.doctor_id.doctorid=:id",nativeQuery = true)
List<Appointment> findAllByDoc(int id);
I am getting this error
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'appointmentController' defined in file [D:\courses\Inellij\appointment\target\classes\com\doctor\appointment\controller\AppointmentController.class]: Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'appointmentServiceImpl': Unsatisfied dependency expressed through field 'appointmentRepository'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'appointmentRepository' defined in com.doctor.appointment.repository.AppointmentRepository defined in @EnableJpaRepositories declared on AppointmentApplication: Invocation of init method failed; nested exception is org.springframework.data.repository.query.QueryCreationException: Could not create query for public abstract java.util.List com.doctor.appointment.repository.AppointmentRepository.findAllByDoc(int)! Reason: Failed to create query for method public abstract java.util.List com.doctor.appointment.repository.AppointmentRepository.findAllByDoc(int)! No property doc found for type Appointment!; nested exception is java.lang.IllegalArgumentException: Failed to create query for method public abstract java.util.List com.doctor.appointment.repository.AppointmentRepository.findAllByDoc(int)! No property doc found for type Appointment!
at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:800) ~[spring-beans-5.3.9.jar:5.3.9]
What should I do, I looked for a lot of solution but nothing works. How should I proceed further?
Solution
By changing my query value=
to name=
, I was able to solve the problem. The latter is used to reference a named query
Answered By - sksk