Please I need some help. I've got 2 entities:
Appointment.class
@Entity
@Table(name = "appointment")
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Appointment {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
@Column(name = "created_date")
private Date createdDate;
@Column(name = "modified_date")
private Date modifiedDate;
@Column(name = "appointment_date")
private LocalDate appointmentDate;
@Column(name = "start_time")
private LocalTime startTime;
private Boolean cancelled;
@ManyToOne
@JoinColumn(nullable = false, name = "client_id")
private Client clientId;
@ManyToOne
@JoinColumn(nullable = false, name = "employee_id")
private Employee employee;
@ManyToOne
@JoinColumn(nullable = false, name = "service_id")
private Service service;
}
And Employee.class
@Entity
@Table(name = "employee")
@NoArgsConstructor
@AllArgsConstructor
public class Employee {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
@Column(name = "first_name")
private String firstName;
@Column(name = "last_name")
private String lastName;
@Column(name = "created_date")
private Date createdDate;
@Column(name = " modified_date")
private Date modifiedDate;
@OneToOne
@JoinColumn(name = "service_id", referencedColumnName = "id")
private Service service;
}
I need to get all the appointments that match with the given startTime
, appointmentDate
and employee
I want to define an abstract method in the interface AppointmentRepo
so that in my AppointmentServices.class
I can call that method with 3 arguments and get the appointment entity.
AppointmentServices.class
appointmentRepo.getAppointmentByDateAndEmployee(date, employee, scheduledHour);
AppointmentRepo interface
@Repository
public interface AppointmentRepo extends JpaRepository<Appointment, Integer>{
@Query("SELECT a FROM Appointment a INNER JOIN a.employee e WHERE a.appointmentDate = :appointment_date AND e = :employee AND s.startTime = :start_time")
public List<Appointment> getAppointmentByDateAndEmployee (@Param("appointment_date") LocalDate appointmentDate,
@Param("employee_id") Employee employee, @Param("start_time") LocalTime startTime);
}
How I have to set my @Query in order to be given an appointment entity that matches with 3 given arguments (a date, and time and a reference to other entity called Employee) Am I doing wrong matching the entire object so I need just to use the id of the Employee entity?
Please help me, and thanks for your time!! Happy Holidays
CodePudding user response:
You can use SQL instead HQL (nativeQuery=true)
DAO Layer
package com.jb.app.repos;
import com.jb.app.beans.Appointment;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;
import java.time.LocalDate;
import java.time.LocalTime;
import java.util.List;
@Repository
public interface AppointmentRepository extends JpaRepository<Appointment, Integer> {
@Query(value = "SELECT * FROM APPOINTMENT WHERE appointment_date = ?1 AND start_time = ?2 AND employee_id = ?3", nativeQuery = true)
List<Appointment> getAppointmentByDateAndEmployee(LocalDate appointmentDate, LocalTime startTime, int employeeId);
}
Service Layer
package com.jb.app.services;
import com.jb.app.beans.Appointment;
import com.jb.app.beans.Employee;
import com.jb.app.repos.AppointmentRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import java.time.LocalDate;
import java.time.LocalTime;
import java.util.List;
@Service
@RequiredArgsConstructor
public class AppointmentServiceImpl implements AppointmentService{
private final AppointmentRepository appointmentRepository;
@Override
public List<Appointment> getAppointmentByDateAndEmployee(LocalDate appointmentDate, LocalTime startTime, Employee e) {
return appointmentRepository.getAppointmentByDateAndEmployee(appointmentDate,startTime,e.getId());
}
}