I 3 Tabellen haben, wie kann ich die Abfrage in Hibernate, wie die Tabellen im Frühjahr verbinden wintern
Termine
id schedule_date subject user_id doctor_id
1 23-12-2016 Fever 1 21
2 24-12-2016 headache 2 22
Verschreibungen
id appointment_id status
1 1 1
2 2 2
user_medications
id prescription_id drug_id status
1 1 1 1
2 1 2 1
beitreten
Jetzt brauche ich das Ergebnis der Benutzer Medikamente
wenn Bedingung user_id = 1 & status = 1
i wird das folgende Ergebnis für die folgenden SQL erhalten.
SELECT um.id, p.id as prescription_id, um.drug_id, um.status FROM `appointments` a
LEFT JOIN `prescriptions` p ON a.id = p.appointment_id
LEFT JOIN `user_medications` um ON p.id = um.prescription_id
WHERE a.patient_id = 30 and um.id != null group by um.id
id prescription_id drug_id status
1 1 1 1
2 1 2 1
Hier ist meine Entity-Klassen:
@Entity
@DynamicInsert
@DynamicUpdate
@Table(name = "appointments")
public class Appointment extends BaseEntity {
private static final long serialVersionUID = 1320944167976543131L;
@Column(name = "schedule_date")
private Date scheduleDate;
@Column(name = "patient_id")
private Long patientId;
@Column(name = "doctor_id")
private Long doctorId;
@Column(name = "subject")
private String subject;
@ManyToOne
@JoinColumn(name = "doctor_id", insertable=false, updatable=false)
private Doctor doctor;
@ManyToOne
@JoinColumn(name = "patient_id", insertable=false, updatable=false)
private User user;
public Date getScheduleDate() {
return scheduleDate;
}
public void setScheduleDate(Date scheduleDate) {
this.scheduleDate = scheduleDate;
}
public String getSubject() {
return subject;
}
public void setSubject(String subject) {
this.subject = subject;
}
public Doctor getDoctor() {
return doctor;
}
public void setDoctor(Doctor doctor) {
this.doctor = doctor;
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
}
Prescription Entity:
@Entity
@DynamicInsert
@DynamicUpdate
@Table(name = "prescriptions")
public class Prescription extends BaseEntity {
private static final long serialVersionUID = 6316326407564010588L;
@Column(name = "user_id")
private Long userId;
@Column(name = "appointment_id")
private Long appointmentId;
@OneToOne
@JoinColumn(name = "appointment_id", insertable=false, updatable=false)
private Appointment appointment;
@OneToMany(mappedBy = "prescription")
private Set<UserMedication> userMedications = new HashSet<>();
@Column(name = "is_self_declared")
private Boolean isSelfDeclared;
public Long getUserId() {
return userId;
}
public void setUserId(Long userId) {
this.userId = userId;
}
public Long getAppointmentId() {
return appointmentId;
}
public void setAppointmentId(Long appointmentId) {
this.appointmentId = appointmentId;
}
public Boolean getIsSelfDeclared() {
return isSelfDeclared;
}
public void setIsSelfDeclared(Boolean isSelfDeclared) {
this.isSelfDeclared = isSelfDeclared;
}
public Appointment getAppointment() {
return appointment;
}
public void setAppointment(Appointment appointment) {
this.appointment = appointment;
}
public Set<UserMedication> getUserMedications() {
return userMedications;
}
public void setUserMedications(Set<UserMedication> userMedications) {
this.userMedications = userMedications;
}
public Doctor getDoctor() {
return doctor;
}
public void setDoctor(Doctor doctor) {
this.doctor = doctor;
}
}
Benutzer Medikamente Einheit:
@Entity
@DynamicInsert
@DynamicUpdate
@Table(name = "prescription_medications")
public class UserMedication extends BaseEntity {
private static final long serialVersionUID = -3853355501806579362L;
@Column(name = "prescription_id")
private Long prescriptionId;
@Column(name = "drug_id")
private Long drugId;
@Column(name = "dosage")
private String dosage;
@ManyToOne
@JoinColumn(name = "prescription_id", insertable=false, updatable=false)
private Prescription prescription;
public Long getPrescriptionId() {
return prescriptionId;
}
public void setPrescriptionId(Long prescriptionId) {
this.prescriptionId = prescriptionId;
}
public Long getDrugId() {
return drugId;
}
public void setDrugId(Long drugId) {
this.drugId = drugId;
}
public String getDosage() {
return dosage;
}
public void setDosage(String dosage) {
this.dosage = dosage;
}
}
Dies ist die Abfrage, habe ich versucht, mit nativen query = true
@Query(value = "select a.id as appointment_id, um.id as user_medication_id, um.drug_id from appointments a "
+ "left join prescriptions p on a.id = p.appointment_id "
+ "left join user_medications um on p.id = um.prescription_id "
+ "where a.patient_id = :userId and um.is_active = :userMedicationStatus and um.id is not null",
nativeQuery = true)
Page<UserMedication> findUserMedications(@Param("userId") Long userId, @Param("userMedicationStatus") Boolean userMedicationStatus, Pageable pageRequest);
Ich erhalte er Fehler folgen,
'userMedicationRepository': Invocation of init method failed; nested exception is org.springframework.data.jpa.repository.query.InvalidJpaQueryMethodException: Cannot use native queries with dynamic sorting and/or pagination in method public abstract org.springframework
kann ich einige Dokumentationsreferenz in HQL/JPQL für die oben genannten – javailike
HQL-Darstellung hinzugefügt bekommen. Wie Sie sehen können, ist der HQL ein bisschen sauberer zu lesen als auch roher SQL. – Naros
kann ich @Query Annotation für diese Art von Funktionalität nicht verwenden,, dies ... @ Query (value = "SELECT um FROM UserMedication JOIN Prescription p WHERE p.userId =: userID AND um.aktiv =: userMediciationStatus ") \t Seite findUserMedications (@param (" userId ") Lange userId, \t \t \t @param (" userMediciationStatus ") int [] userMediciationStatus, Pageable pageRequest); –
javailike