Hi i'm new with Spring Boot and I don't know how to represent my two foreign keys can someone help me to understand.
CREATE TABLE user
(
id_user INT PRIMARY KEY NOT NULL,
lastname VARCHAR(100) NOT NULL,
name VARCHAR(100) NOT NULL
)
CREATE TABLE email
(
id_email INT PRIMARY KEY NOT NULL,
object VARCHAR(255) NOT NULL,
message VARCHAR(255) NOT NULL,
id_sender INT NOT NULL,
id_recipient INT NOT NULL,
FOREIGN KEY (id_sender) REFERENCES user(id_user),
FOREIGN KEY (id_recipient) REFERENCES user(id_user),
)
How to represent the Foreign key relation :/
CodePudding user response:
In your email entity class
@OneToOne(cascade= CascadeType.ALL)
@JoinColumn(name = "id_sender", referencedColumnName = "id_user")
private User Id_Sender;
@JoinColumn annotation is the key and it will tell this email entity is the owner of the field and reference to the Entity User with column id_user
CodePudding user response:
u dont need "not null" when u use PRIMARY KEY