reading-notes


Project maintained by sanaa-almoghraby Hosted on GitHub Pages — Theme by mattgraham

Many to many relationships

3. Database Setup

We also need to create the employee and project tables along with the employee_project join table with employee_id and project_id as foreign keys:

CREATE TABLE employee ( employee_id int(11) NOT NULL AUTO_INCREMENT, first_name varchar(50) DEFAULT NULL, last_name varchar(50) DEFAULT NULL, PRIMARY KEY (employee_id) ) ENGINE=InnoDB AUTO_INCREMENT=17 DEFAULT CHARSET=utf8;

CREATE TABLE project ( project_id int(11) NOT NULL AUTO_INCREMENT, title varchar(50) DEFAULT NULL, PRIMARY KEY (project_id) ) ENGINE=InnoDB AUTO_INCREMENT=18 DEFAULT CHARSET=utf8;

CREATE TABLE employee_project ( employee_id int(11) NOT NULL, project_id int(11) NOT NULL, PRIMARY KEY (employee_id,project_id), KEY project_id (project_id), CONSTRAINT employee_project_ibfk_1 FOREIGN KEY (employee_id) REFERENCES employee (employee_id), CONSTRAINT employee_project_ibfk_2 FOREIGN KEY (project_id) REFERENCES project (project_id) ) ENGINE=InnoDB DEFAULT CHARSET=utf8;

4. The Model Classes

The @ManyToMany annotation is used in both classes to create the many-to-many relationship between the entities. This association has two sides i.e. the owning side and the inverse side. In our example, the owning side is Employee so the join table is specified on the owning side by using the @JoinTable annotation in Employee class. The @JoinTable is used to define the join/link table. In this case, it is Employee_Project. The @JoinColumn annotation is used to specify the join/linking column with the main table. Here, the join column is employee_id and project_id is the inverse join column since Project is on the inverse side of the relationship.

In the Project class, the mappedBy attribute is used in the @ManyToMany annotation to indicate that the employees collection is mapped by the projects collection of the owner side. **************** Thinking about security is like thinking about where to ride your motorcycle: the safe places are no fun, and the fun places are not safe. I shall ride wherever my spirit takes me, and I shall find my Gigantic Martian Insect Party, and I will, uh, probably be rent asunder by huge cryptozoological mandibles, but I will die like Thomas Jefferson: free, defiant, and without a security label.***