简体   繁体   中英

How to set up a bidirectional relationship in MySql?

I am working on a web application in which, upon registration, the patient chooses a doctor to sign up for. After registration, doctor_id is immediately assigned to the patient. However, the patient is not assigned to a doctor. Is it a misallocation of a relationship in MySql?

@Entity
@Getter
@Setter
public class Doctor {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private long id;

    @NotBlank
    @Pattern(regexp = "^[A-Za-z]*$", message = "Wpisz poprawne imie.")
    private String firstName;

    @NotBlank
    @Pattern(regexp = "^[A-Za-z]*$", message = "Wpisz poprawne nazwisko.")
    private String lastName;

    @Pattern(regexp = "(?=^.{8,}$)((?=.*\\d)|(?=.*\\W+))(?![.\\n])(?=.*[A-Z])(?=.*[a-z]).*$", message = "Haslo musi posiadac przynajmniej 1 duza litere, 1 cyfre, 1 znak specjalny oraz minimum 8 znakow dlugosci.")
    private String password;

    @Transient
    private String repassword;

    @Email
    @NotEmpty
    @NotNull
    private String email;

    @Pattern(regexp = "^[5-8]\\d{8}$", message = "Podaj prawidlowy numer telefonu.")
    @NotNull
    @Min(9)
    private String phoneNumber;

    @OneToMany
    List<Patient> patients = new ArrayList<>();

    public String getFullName() {
        return firstName + " " + lastName;
    }
}
package pl.smile.SmileApp.entity;
import lombok.*;
import org.hibernate.validator.constraints.UniqueElements;
import org.hibernate.validator.constraints.pl.PESEL;

import javax.persistence.*;
import javax.validation.constraints.*;

@Entity
@Getter
@Setter
@ToString
public class Patient {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private long id;

    @NotBlank
    @Pattern(regexp = "^[A-Za-z]*$", message = "Wpisz poprawne imie.")
    private String firstName;

    @NotBlank
    @Pattern(regexp = "^[A-Za-z]*$", message = "Wpisz poprawne nazwisko.")
    private String lastName;

    @Pattern(regexp = "(?=^.{8,}$)((?=.*\\d)|(?=.*\\W+))(?![.\\n])(?=.*[A-Z])(?=.*[a-z]).*$", message = "Haslo musi posiadac przynajmniej 1 duza litere, 1 cyfre, 1 znak specjalny oraz minimum 8 znakow dlugosci.")
    private String password;

    @Transient
    private String repassword;

    @Email
    @NotBlank
    private String email;

    @PESEL()
    private String pesel;

    @Pattern(regexp = "^[5-8]\\d{8}$", message = "Podaj prawidlowy numer telefonu.")
    @NotNull
    @Min(9)
    private String phoneNumber;

    @AssertTrue
    private boolean confirmed;

    @ManyToOne
    private Doctor doctor;

    public String getFullName() {
        return firstName + " " + lastName;
    }


}

and my DB: PatientDB

DoctorDB

It depends how you design your database. If you set the column doctor_id of the Patient table as foreign_Key of the Primary Key of the Doctor table you should be more than fine. The relationship will be 1 doctor to multiple patients. If you want to have multiple doctors to multiple patients then you will need an associative table like you have in doctor_patients, but still you will have to link the doctor_id column with the primary key of the Doctor Table and patients_id with the primary key of Patient table (the column named simply "id"). From the code it seems like you want 1 doctor to many patiens, so you don't need an associative table doctor_patients, you just need to link the primary key of the Doctor table to the doctor_id column of the Patient table. You also need the field Doctor ID in the Patient Class. If you then want to retrieve the list of patients of your doctor simply query the Patient table with doctor ID.

listOfPatients = dbContext.Patients.Where(Function(x) x.doctor_id = 1).ToList

i don't remember the exact syntax for java entity queries but something like that should work

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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