[英]JPQL @Query for a ManyToOne relationship
我需要一些幫助。 我有 2 個實體:
約會類
@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;
}
和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;
}
我需要獲取與給定的startTime
、 appointmentDate
和employee
匹配的所有約會 我想在接口AppointmentRepo
中定義一個抽象方法,以便在我的AppointmentServices.class
中我可以使用 3 個參數調用該方法並獲取約會實體。
約會服務類
appointmentRepo.getAppointmentByDateAndEmployee(date, employee, scheduledHour);
AppointmentRepo 接口
@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);
}
我必須如何設置我的@Query 才能獲得與 3 個給定參數(日期和時間以及對其他名為 Employee 的實體的引用)匹配的約會實體我是否在匹配整個對象時做錯了所以我只需要使用 Employee 實體的 id?
請幫助我,感謝您的寶貴時間! 節日快樂
您可以使用 SQL 而不是 HQL (nativeQuery=true)
道層
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);
}
服務層
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());
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.