First there are two entities present here department and employee.
Second, we have to understand what ManyToOne
means here, as you said, many employees could be there in a single department.
Mapping would be like, please note that this a snippet only:
@Entity
@Table(name="EMPLOYEE")
public class Employee {
@Id
@GeneratedValue
@Column(name="employee_id")
private Long employeeId;
@Column(name="firstname")
private String firstname;
@Column(name="lastname")
private String lastname;
@ManyToOne
@JoinColumn(name="department_id")
private Department department;
// Getter and Setter methods
}
@Entity
@Table(name="DEPARTMENT")
public class Department {
@Id
@GeneratedValue
@Column(name="department_id")
private Long departmentId;
@Column(name="department_name")
private String departmentName;
@OneToMany(mappedBy="department")
private Set<Employee> employees;
// Getter and Setter methods
}
Since there is a bi-directional
relationship here one side would be a owner of the relationship here.
The annotation @JoinColumn
indicates that this entity is the owner of the relationship. That is, the corresponding table has a column with a foreign key to the referenced table, whereas the attribute mappedBy
indicates that the entity in this side is the inverse of the relationship, and the owner resides in the "other" entity.
More on what does mapppedby
do can be found here.