class Organization extends Party {

    public List<Department> listChildDepartments() {
        String jpql = "select c from OrgLineMgmt o join o.responsible c " +
                "where o.commissioner = :commissioner and TYPE(c) = Department";
        return getRepository().createJpqlQuery(jpql).addParameter("commissioner", this).list();
    }

}

class OrganizationTest extends Party {

    @Test
    public final void listChildDepartments() {

        OrganisationUtils organisationUtils = new OrganisationUtils();
        Date date = DateUtils.date(2012, 1, 3);
        Company headquarter = organisationUtils.createCompany("总公司", date);
        headquarter.save();
        Department financial = organisationUtils.createDepartment("财务部", headquarter, date);
        Company company1 = organisationUtils.createCompany("分公司1", headquarter, date);
        Department dept1 = organisationUtils.createDepartment("核算科", financial, date);
        Department dept2 = organisationUtils.createDepartment("财务部2", company1, date);

        List<Department> departments = headquarter.listChildDepartments();

        assertTrue(departments.contains(financial));
        assertFalse(departments.contains(company1));
        assertFalse(departments.contains(dept1));
        assertFalse(departments.contains(dept2));
    }

}