Reputation: 306
I have a form that tracks employee projects and the fields are: employee name (the project requestor), employee assigned to complete the task, task description, and a field with the option to reassign the task to someone else. I have a SQL employee table with the name and email of all employees. Then I have a table for the projects. I have it set up so all three columns refer to the employee table via Foreign keys.
I'm experiencing a mapping issue with Spring JPA. One project can have many employees because there are three employee related fields. Is this right?
Or should it be a many to many because an employee can have many projects and each project could have three employess stored (same or different) that it should be a many to many relationship.
The one to many seems right but then also is confusing because there will only be one employee listed in the employee name, one in the assigned to and one in the reassigned to field. Is one-to-many right?
public class ProjectTracker {
@ManyToMany
@JoinColumn(name = "employeeName")
private Employee employeeName;
private String taskDetails;
@ManyToMany
@JoinColumn(name = "employeeName")
private Employee assignedTo;
@ManyToMany
@JoinColumn(name = "employeeName")
private Employee reassignedTo;
}
public class Employee {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "employeeId")
private Long employeeId;
private String employeeName;
private String emailAddress;
private Boolean isAssignee=false;
private Boolean isRequestor=true;
}
Upvotes: 0
Views: 160
Reputation: 8206
One project can have many employees because there are three employee related fields. Is this right?
No.
The number of fields is not relevant. Here you have three different associations. Given an employee you need an association for the project they have requested, an association for the projects the've been assigned to and an association for the projects they've been reassigned to. All these associations are all one-to-many because an employee can be the requester for many different projects and the same is true for the other roles.
A single project, a row on the db, can only have one requester, one employee it has been assigned to and one person it has been reassigned to. Each one of them is a many-to-one.
ProjectTracker
should probably look something like:
public class ProjectTracker {
@Id
private Long id;
@ManyToOne
@JoinColumn(name = "requestedBy_id")
private Employee employeeName;
private String taskDetails;
@ManyToOne
@JoinColumn(name = "assignedTo_id")
private Employee assignedTo;
@ManyToOne
@JoinColumn(name = "reassignedTo_id")
private Employee reassignedTo;
}
Given a project tracker, you should have all the information you need. I would suggest to read the Hibernate ORM documentation chapter about associations. It contains many examples of all the different mappings.
Upvotes: 1