So let's say you have
class Employee {
@ManyToOne
@JoinColumn
private Department department;
}
class Department {
}
class EmployeeGroup {
@OneToMany
@JoinTable
private Set<Employee> employees;
@ManyToOne
@JoinColumn
private Department department;
}
How would you ensure that all employees from a specific group, are from the same Department as the EmployeeGroup's department ?
I've tried with array of @JoinColumn
within @JoinTable
... but It seems that hibernate can't share in link table the same column for fk_department_id in both Foreign keys.
What I expect to get is : a link table with three columns, fk_group_id, fk_employee_id, fk_department_id, and 2 FK (department_id, employee_Id) to employee table, and (department_id, group_id) to emp_group table.
Is it possible without ComplexPK?
Business case : you have two departments, IT and Finance, 3 groups, Java Group and .NET group which belong to IT department, HR group which belong to Finance department. You have 5 employees. all of them are in IT department, two of them also in Java group, two of them in .NET group, and one of them is an internship member which does not belong to any groups yet, but it is in the same department. Now I want to ensure that when you take 1 employee from IT department and say employee.getGroups(), all of them are also IT groups and the collection does not contain accidentally the HR group.