Entity Relationship Mapping
Cascade Types
-
CascadeType.PERSIST: Saving the parent also saves the child. -
CascadeType.MERGE: Updating the parent also updates the child. -
CascadeType.REMOVE: Deleting the parent also deletes the child. -
CascadeType.REFRESH: Refreshing the parent also refreshes the child from the DB. -
CascadeType.DETACH: Detaching the parent also detaches the child from the persistence context. -
CascadeType.ALL: Applies all of the above.
1. One-to-One (1:1)
- One entity is associated with exactly one other entity.
- Example: A User has one Profile.
Entity example
Entity Example
@Entity
public class Profile{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int pid;
private String email; private String phone;
//Getters and Setters
}
@Entity
public class User{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int uid;
private String name; private String gender;
@OneToOne(cascade =CascadeType.ALL)
private Profile profile;
//Getters and Setters
}
here,
cascade = CascadeType.ALL: any operation onUserwill also apply to itsProfile
SQL example
SQL Example
CREATE TABLE profile (
pid INT AUTO_INCREMENT PRIMARY KEY,
phone VARCHAR(20) NOT NULL,
email VARCHAR(255) NOT NULL,
);
CREATE TABLE user (
uid INT AUTO_INCREMENT PRIMARY KEY,
pid INT UNIQUE,
name VARCHAR(255) NOT NULL,
gender VARCHAR(255) NOT NULL,
FOREIGN KEY (pid) REFERENCES profile(pid) ON DELETE CASCADE
);
here ,
- A profile must exist before inserting a user(because of FK).
- ON DELETE CASCADE( to maintain data integrity)
- Deleting a profile deletes the user automatically
- but deleting a user does not delete the profile.
- cascade only works one way here.
2. One-to-Many / Many-to-One (1:N / N:1)
- One entity is related to multiple other entities.
- Example: A user can place many orders, but each order belongs to one user.
Entity Example
here ,
@JoinColumn(name = "user_uid"):
- Defines the foreign key column inside the Orders table.
- the Orders table will have a column
user_uidpointing toUser(uid).
SQL Example
3. Many-to-Many (M:N)
- Multiple entities can be associated with multiple others.
- Example : User has many Roles, and one Role has many Users
The join table is required in a many-to-many relationship because relational databases cannot directly represent M:N relationships between two tables.
Entity Example
@Entity
public class Roles{
@Id
private int rid;
private String name;
}
@Entity
public class User{
@Id
private int uid;
private String name;
@ManyToMany(cascade = CascadeType.MERGE, fetch = FetchType.EAGER)
@JoinTable(name = "user_roles_join", joinColumns = @JoinColumn(name = "uid"),
inverseJoinColumns = @JoinColumn(name = "rid"))
List<Roles> roles = new ArrayList<>();
}
here ,
@JoinTable:
- Defines the third table that connects User and Roles.
- Table name:
user_roles_join joinColumns (uid): Refers to the User entity’s primary key.inverseJoinColumns (rid): Refers to the Roles entity’s primary key.
SQL Example
CREATE TABLE user (
uid INT PRIMARY KEY,
name VARCHAR(255) NOT NULL
);
CREATE TABLE roles (
rid INT PRIMARY KEY,
name VARCHAR(255) NOT NULL
);
CREATE TABLE user_roles_join (
uid INT NOT NULL,
rid INT NOT NULL,
PRIMARY KEY (uid, rid),
FOREIGN KEY (uid) REFERENCES user(uid),
FOREIGN KEY (rid) REFERENCES roles(rid)
);
Github Code : Entity Relationship Mapping