简体   繁体   English

Hibernate使用注释将多个映射到一个表

[英]Hibernate Mapping many to one table with annotations

How would I create a HQL query to select the patients mutiple email address when provided with the fields patient firstname and lastname. 当提供患者名字和姓氏字段时,我将如何创建一个HQL查询来选择患者多个电子邮件地址。 tables and classed provided below. 表和分类如下。

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;

Here I create a patient class and and set all the fields and annotate the relationship Patient Class 在这里,我创建一个患者类并设置所有字段并注释关系“ 患者类”

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 

Patient Email Class 患者电子邮件类别

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 

I'm struggling to find the soloution. 我正在努力寻找这种解决方法。

Since you are interested in the PatientEmailAddress instances related to one Patient you could query for those entities using something like: 由于您对与一个患者相关的PatientEmailAddress实例感兴趣,因此可以使用以下方式查询这些实体:

select t from PatientEmailAddress t 
    join t.patient p
where p.firstName = :firstName and p.lastname = :lastname

Note that this will return instances of PatientEmailAddress and not of Patient. 请注意,这将返回PatientEmailAddress的实例,而不是Patient的实例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM