Hi all,
I need to define two relationships between two objects and am not sure which is the best approach to do the same.
I have two objects, employees and departments, which share a many-to-many relationship.
Apart from this I also need to know who (employee) is the creator of a department, which implies a one-to-many relationship between employee and departments.
If in my Employee class, I contain two sets, one for createdDepartments and one for belongToDepartments and use the following XDoclet tags in my employee class
/**
* @hibernate.set table="EMPLOYEE_DEPARTMENT_LINK"
* @hibernate.collection-key column="EMP_ID"
* @hibernate.collection-many-to-many
* class="model.Department"
* column="DEPT_ID"
*
* @return Returns the department collection associated with this employee.
*/
public Set getBelongToDepartments () {
return this._belongToDepartments;
}
And
/**
* @hibernate.set
* @hibernate.collection-key column="CREATOR_EMP_ID"
* @hibernate.collection-one-to-many
* class="model.Department"
*
* @return Returns the department collection created by this employee.
*/
public Set getCreatedDepartments () {
return this._createdDepartments;
}
The mapping is created and tables are also created. When I create an employee and add department objects to it under the respective sets, it adds the new employee, the departments in the tables. In the link table an entry is made only for departments the employee belongs to, and for the created departments, a foreign key is introduced in department table.
The problem with this approach is that if I have to get a complete list of departments with which the employee is associated then I can not get it only from the link table but would have to go to department table also.
Some of the other options that we thought of are:
1) Define the many to many relationship from employee object and many to one from department object. Each department object would have an instance of its creator employee object. The only problem with this that we thought of was that this could introduce cyclic association, in the sense that a department contains an employee and the employee in turn contains n other departments which contain their own creator employee objects. When we load the department or employee object, it may possibly load the entire object graph in memory (it should not but theoretically possible !!!!).
2) If we go by the caveat emperor example, and use a link class like Categorized Items and store the association between employee and department in the link table only and add another column (additional information) for creatorFlag then also our purpose is served and from one table we get the entire association set. But in this case, suppose a mapping is deleted, we may loose information about the creator of a department.
Any inputs from you all on which of the three approaches is better (defining both in employee class using two different sets, defining it in both objects (option 1) or caveat emperor approach)?
I hope I was able to state my problem with sufficient detail to come across clearly.
Thanks in advance,
Regards,
SK
|