I have the following requirements
Tables ======= CREATE TABLE EMPLOYEE ( ID NUMBER(10, 0) NOT NULL, NAME VARCHAR2(80) NOT NULL, CONSTRAINT EMPLOYEE_PK PRIMARY KEY (ID) );
CREATE TABLE WORKS ( ID NUMBER(10, 0) NOT NULL, EMP_ID NUMBER(10, 0) REFERENCES EMPLOYEE(ID), WORKS_FOR_ID NUMBER(10, 0) REFERENCES EMPLOYEE(ID), CONSTRAINT WORKS_PK PRIMARY KEY (ID) );
Here the WORKS table has been created so that it enables an Employee to work for many Employees.
I have the following classes
Employee.java ============= public class Employee {
private Long id;
private String name; private Set<Works> works; /**Setter and getters for them**/ .... .... } employee.hbm ============ <hibernate-mapping> <class name="com.xxx.Employee" table="EMPLOYEE"> <id name="id" type="long"> <column name="ID" not-null="true"/> <generator class="native"> <param name="sequence">EMPLOYEE_SEC_NBR</param> </generator> </id> <property name="name" column="NAME" not-null="true" /> <set name="works" inverse="true"> <key column="EMP_ID"/> <one-to-many class="com.xxx.Works"/> </set> </class> </hibernate-mapping>
what i am trying to achieve is to get rid of the class Works and some how fetch employees a given employee works for
Currently with the code i have i need to do Set<Employee> worksForSet = new HashSet(); for(Works work: employee.getWorks()) { worksForList.add(work.getWorksForEmployee()); }
I want to something like Set<Employee> worksForSet = employee.getWorksForEmployees();
One way is to write the code in the getWorksForEmployees() method to form the Set using the Works Set. But this way i would end up still keeping the setter getter for works. I dont want to do this.
Is there a cleaner approach ?
|