Hello,
I needed to create many-to-many relation between boss and employee which are instances of same class. Every boss can have one or more employees and vice versa. I created two tables, one operator table which holds data of operator and second table hierarchy to mantain relation between them. I created many-to-many relation in hibernate like this:
<set name="bosses" inverse="true" lazy="true" table="hierarchy" fetch="select"> <key> <column name="EmployeeID" not-null="true" /> </key> <many-to-many column="ManagerID" class="operator" /> </set> <set name="employees" inverse="true" lazy="true" table="hierarchy" fetch="select"> <key> <column name="ManagerID" not-null="true" /> </key> <many-to-many column="EmployeeID" class="operator" /> </set>
Everything works fine except one thing. When i add record to hierarchy table i got set of bosses updated in all operators but set of employees is not updated for some reason. I persist hierarchy like this: Hierarchy hierarchy = new Hierarchy(employee.getOperatorId(), boss.getOperatorId()); HierarchyDAO.persist(hierarchy);
What is strange is when i restart application, everything looks fine and both sets are updated properly.
I tried to add manually employee to boss every time when i persist hierarchy object with: boss.getEmployees().add(operator); and it works but i doubt it is needed.
Does someone have idea what i am doing wrong.
Best regards.
|