在提供患者名字和姓氏字段时,我将如何创建HQL查询以选择患者多个电子邮件地址。表格和分类如下。
CREATE TABLE Patient (
Patient_Id INT NOT NULL AUTO_INCREMENT,
First_Name VARCHAR(55) NOT NULL,
Middle_Name VARCHAR(55),
Last_Name VARCHAR(55) NOT NULL,
Is_Male TINYINT NOT NULL,
Medical_Information VARCHAR(5000),
Date_Of_Birth DATE,
Row_Create DATETIME NOT NULL,
Row_LastUpdate DATETIME,
PRIMARY KEY ( Patient_id),
INDEX name(First_Name, Last_Name)
) ENGINE=INNODB;
CREATE TABLE Address (
PatientAddress_Id INT NOT NULL AUTO_INCREMENT,
Patient_Id INT NOT NULL,
House_Name VARCHAR(100),
House_Number VARCHAR(20),
Street VARCHAR(70) NOT NULL,
City VARCHAR(70) NOT NULL,
Postcode VARCHAR(8) NOT NULL,
County VARCHAR(50) NOT NULL,
Country VARCHAR(60) NOT NULL,
Row_Create DATETIME NOT NULL,
Row_LastUpdate DATETIME,
PRIMARY KEY ( PatientAddress_id),
FOREIGN KEY (Patient_Id)
REFERENCES Patient(Patient_Id)
ON UPDATE CASCADE ON DELETE RESTRICT,
INDEX HouseNum_Street(House_Number, Street),
INDEX PatientID_postcode(Patient_Id, Postcode)
)ENGINE=INNODB;
在这里,我创建一个耐心的课程,并设置所有字段并注释关系 患者类别
import org.hibernate.annotations.CreationTimestamp;
import org.hibernate.annotations.UpdateTimestamp;
import org.jetbrains.annotations.Contract;
import javax.persistence.*;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.HashSet;
import java.util.Objects;
import java.util.Set;
@NamedQueries(
value = {
@NamedQuery(
name = "findPatientByFirstName",
query = "from Patient p where p.firstName = :name"
),
@NamedQuery(
name = "findPatientByLastName",
query = "from Patient p where p.lastName = :name"
),
@NamedQuery(
name = "findPatientByDOB",
query = "from Patient p where p.dob = :dob"
),
@NamedQuery(
name = "findPatientByFullName",
query = "from Patient p where p.firstName = :firstName AND p.lastName = :lastName"
)
}
)
@Entity
@Table(name = "patient")
public class Patient {
@Id @GeneratedValue(strategy = GenerationType.IDENTITY)
@Column (name = "Patient_id")
private Integer id;
@Column( name="Date_Of_Birth")
private LocalDate dob;
@Column( name="First_Name")
private String firstName;
@Column( name="Middle_Name")
private String middleName;
@Column( name="Last_Name")
private String lastName;
@Column( name="Medical_Information")
private String medicalInformation;
@Column( name="Is_Male")
private boolean isMale;
@Column (name = "Row_Create")
@CreationTimestamp
private LocalDateTime createDateTime;
@Column (name = "Row_LastUpdate")
@UpdateTimestamp
private LocalDateTime updateDateTime;
public Patient() {
}
public Patient(LocalDate dob, String firstName, String lastName, String medicalInformation, boolean isMale) {
this.dob = dob;
this.firstName = firstName;
this.lastName = lastName;
this.medicalInformation = medicalInformation;
this.isMale = isMale;
}
//Setters and getters
患者电子邮件类别
import org.hibernate.annotations.CreationTimestamp;
import org.hibernate.annotations.Fetch;
import org.hibernate.annotations.FetchMode;
import org.hibernate.annotations.UpdateTimestamp;
import org.jetbrains.annotations.Contract;
import javax.persistence.*;
import java.time.LocalDateTime;
import java.util.Objects;
@NamedQueries(
value = {
@NamedQuery(
name = "findEmailByPatientsName",
query = "SELECT pE.patient from PatientEmailAddress pE
join pE.emailAddress e where pE.patient = :patient"
// select cus.mail from Customer cus join cus.mainOrder man WHERE man.id = 1
)
}
)
@Entity
@Table(name = "patientEmailAddress")
public class PatientEmailAddress {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "PatientEmailAddress_Id", unique = true)
private Integer id;
@Column(name = "Email_Address", unique = true)
private String emailAddress;
@ManyToOne(cascade = CascadeType.ALL)
@Fetch(FetchMode.JOIN)
@JoinColumn(name = "Patient_id")
private Patient patient;
@Column (name = "Row_Create")
@CreationTimestamp
private LocalDateTime createDateTime;
@Column (name = "Row_LastUpdate")
@UpdateTimestamp
private LocalDateTime updateDateTime;
public PatientEmailAddress() {
}
public PatientEmailAddress(String emailAddress, Patient patient) {
this.emailAddress = emailAddress;
this.patient = patient;
}
//setters and getters
我正在努力寻找这种解决方案。
答案 0 :(得分:0)
由于您对与一名患者相关的PatientEmailAddress实例感兴趣,因此可以使用以下方式查询这些实体:
select t from PatientEmailAddress t
join t.patient p
where p.firstName = :firstName and p.lastname = :lastname
请注意,这将返回PatientEmailAddress而不是Patient的实例。