Monday 26 December 2022

Hibernate 6: @ManyToMany: many to many association

@ManyToMany association maintain a link table to join two entities. Like the @OneToMany, @OneToOne associations, @ManyToMany can be either unidirectional or bidirectional.

 

Unidirectional @ManyToMany association

@Entity
@Table(name = "project")
public class Project {
    @Id
    private int projectId;

    private String projectName;

    ...........
    ...........
}

@Entity
@Table(name = "employee")
public class Employee {
    @Id
    private int id;

    private String name;

    @ManyToMany(cascade = { CascadeType.PERSIST, CascadeType.MERGE })
    private List<Project> projects = new ArrayList<>();

    ..........
    ...........
}

 

Above snippet generates following DDL.

  create table employee (
       id integer not null,
        name varchar(255),
        primary key (id)
    )

    create table employee_project (
        Employee_id integer not null,
        projects_projectId integer not null
    )

    create table project (
        projectId integer not null,
        projectName varchar(255),
        primary key (projectId)
    )

    alter table if exists employee_project 
       add constraint FK84if10lpuqoq270kixummx39k 
       foreign key (projects_projectId) 
       references project

    alter table if exists employee_project 
       add constraint FKcfge3ley7bbpogw8dur1p5q35 
       foreign key (Employee_id) 
       references employee

Bidirectional @ManyToMany association

A bidirectional @ManyToMany association has an owning and a mappedBy side.

@Entity
@Table(name = "employee")
public class Employee {
  @Id
  private int id;

  private String name;

  @ManyToMany(cascade = { CascadeType.PERSIST, CascadeType.MERGE })
  private List<Project> projects = new ArrayList<>();

  public void addProject(Project project) {
    projects.add(project);
    project.getEmployees().add(this);
  }

  public void removeAddress(Project project) {
    projects.remove(project);
    project.getEmployees().remove(this);
  }

}


@Entity
@Table(name = "project")
public class Project {
  @Id
  private int projectId;

  private String projectName;

  @ManyToMany(mappedBy = "projects")
  private List<Employee> employees = new ArrayList<>();

}

Above entities generate below DDL.

    create table employee (
       id integer not null,
        name varchar(255),
        primary key (id)
    )

    create table employee_project (
       employees_id integer not null,
       projects_projectId integer not null
    )

    create table project (
       projectId integer not null,
        projectName varchar(255),
        primary key (projectId)
    )

    alter table if exists employee_project 
       add constraint FK84if10lpuqoq270kixummx39k 
       foreign key (projects_projectId) 
       references project

    alter table if exists employee_project 
       add constraint FK79jew72qqettaqpet0s7h9rda 
       foreign key (employees_id) 
       references employee

To preserve synchronicity between both sides, it’s good practice to provide helper methods (addProject, removeProject) for adding or removing child entities.

 

Previous                                                    Next                                                    Home

No comments:

Post a Comment