I have some doubts in JPA. I have one Employee table and Project table. When the values are saved in Employee and Project tables have no relation. The scenario is first, admin should be able to create the Employee and Project. Later project manager assigns each employee to different project. Here how do I write the values to the table called employee_project_assignment?
employee_id project_id
----------- ----------
100 ABC987
100 DEF876
101 ABC987
102 DEF876
The UI will be like the following way.
When the EmployeeProjectAssignment form comes,Project Manager selects one project from different projects from the dropdown. And he selects multiple employees whom he wants to that project. In this
way. So how do I create the entity for this EmployeeProjectAssignment form?
The above table is a pure join table used to materialize the ManyToMany association between Employee and Project.
The Employee entity could have a collection of Projects, and/or the Project entity could have a collection of Employees (i.e. teh relation can be unidirectional from Employee to Project, or unidirectional from Project to employee, or bidirectional).
Decide which kind of association better fits your needs (in general, a bidirectional one makes life easier). If bidirectional, choose which side is the owner of the association (the owner side has a @JoinTable annotation, and the other side has a mappedBy attribute), and then in your form handling method, populate the collections. The owning side is the side that must be populated for JPA to persist the association.
Suppose the owning side is Employee: