Well, this is just an axample, i've really got a quite complex structure of related information that would take too long to dechipher, and it would just be a distraction from the core point...
I'll post my entire structure below (its
only got the relevant parts to solve the problem 0% clutter ;) )
The problem is this, I want to put all employees that belong to a department, lets say Sales, into one team and all management into another. But since employees can belong to several departments (A Sales manager) serveral of them will have > 1 Department.
To ensure dataintegrity when an employee is loaded into a team the Employee object still has to maintain all its Departments. Otherwise an update would have some interesing side-effects. So i cannot just filter out any departments that does not match.
For the same reason I could not just put a department in the Team, as there should be Employees with >1 departmens present.
What i'd like to find out is if this filter has any real chance of working ;
Code:
<filter name="departmentFilter"
condition=" EMPLOYEE_ID in (select distinct dept.EMPLOYEE_ID from DEPARTMENT dept where :filterAttr = channel.DEPARTMENT_ID and EMPLOYEE_ID = dept.EMPLOYEE_ID)" />
Or if i have to write a custom query to do this. That would be no problem, but i'd really like to manage this within the hbm file and only expose a set of filters that developers can use.
This is my mapping, note the filter in the Employee Class..
Code:
<hibernate-mapping>
<!-- 'Team' object contains a list of employees filtered out on a departments -->
<class name="com.org.Team" table="TEAM" lazy="false" dynamic-update="true">
<cache usage="read-write" />
<id column="id" name="ID" type="long" unsaved-value="0">
<generator class="increment"></generator>
</id>
<!--
This set should contain a set of employees, filtered based on a given department.
An employee that exist in the list has All their departments present.
-->
<set name="emloyees" inverse="true" cascade="all-delete-orphan" lazy="false">
<key column="EMPLOYEE_ID" />
<one-to-many class="com.org.Employee" />
</set>
</class>
<!--
'Employee' object
-->
<class name="com.org.Employee" table="employee" lazy="false" dynamic-update="true">
<id column="EMPLOYEE_ID" name="id" type="long">
<generator class="assigned"></generator>
</id>
<set name="departments" inverse="true"
cascade="all-delete-orphan" lazy="false">
<key column="DEPARTMENT_ID" />
<one-to-many class="com.org.Department" />
</set>
<!--
Filter out any Employee that does NOT have the desired department
-->
<filter name="departmentFilter"
condition=" EMPLOYEE_ID in (select distinct dept.EMPLOYEE_ID from DEPARTMENT dept where :filterAttr = dept.DEPARTMENT_ID and EMPLOYEE_ID = dept.EMPLOYEE_ID)" />
</class>
<!--
'Department' class,
-->
<class name="com.org.Department" table="DEPARTMENT" lazy="false" dynamic-update="true">
<cache usage="read-write" />
<id column="DEPARTMENT_ID" name="cartItemChannelId">
<generator class="assigned" />
</id>
<many-to-one name="employee" column="EMPLOYEE_ID" not-null="true" lazy="false" />
<!--
Applies no filters to departments, an employee will always have all their departments present.
-->
</class>
<filter-def name="departmentFilter">
<filter-param name="filterAttr" type="integer" />
</filter-def>
</hibernate-mapping>
//Johan