I have an employee search form which can take any combination of a number of employee attributes. One of them is the cost centre an employee belongs to.
My Employee class has an abstract CostCentre object. The CostCentre has 2 concrete subclasses, InternalCostCentre and ExternalCostCentre. This is mapped as a table-per-subclass like this:
Code:
<class name="au.com.woolworths.hrportal.costcentre.CostCentre" table="PYRL_COST_CNTR">
...
<joined-subclass name="au.com.woolworths.hrportal.costcentre.InternalCostCentre" table="INTL_COST_CNTR">
<key column="PYRL_COST_CNTR_ID" />
<property name="costCentreNumber" column="COST_CNTR_NO" access="field" />
<property name="name" column="COST_CNTR_NM" access="field" />
</joined-subclass>
<joined-subclass name="au.com.woolworths.hrportal.costcentre.ExternalCostCentre" table="EXTL_COST_CNTR">
<key column="PYRL_COST_CNTR_ID" />
<property name="costCentreNumber" column="EXTL_COST_CNTR_NO" access="field" />
<property name="name" column="EXTL_COST_CNTR_NM" access="field" />
</joined-subclass>
Employee has a mapping like this:
Code:
<class name="au.com.woolworths.hrportal.employee.Employee" table="EMP">
...
<many-to-one name="costCentre" class="au.com.woolworths.hrportal.costcentre.CostCentre" column="PYRL_COST_CNTR_ID" access="field" />
...
The costCentreNumber is an attribute of the parent class and I want to be able to search on costCentreNumber, but the search needs to cater for the fact that the type of cost centre (whether internal or external) is unknown. I tried this
Code:
Criteria criteria = getSession().createCriteria(Employee.class);
Criteria costCentreCriteria = criteria.createCriteria("costCentre");
DetachedCriteria internal = DetachedCriteria.forClass(InternalCostCentre.class);
internal.add(Restrictions.eq(("costCentreNumber"), employee.getCostCentre().getCostCentreNumber()));
DetachedCriteria external = DetachedCriteria.forClass(ExternalCostCentre.class);
external.add(Restrictions.eq(("costCentreNumber"), employee.getCostCentre().getCostCentreNumber()));
costCentreCriteria.add(Subqueries.exists(internal)).add(Subqueries.exists(external));
I get a ClassCastException. I'm probably completely off the mark here, but have tried a number of different things, without success.
Any suggestions?
Thanks
Alan