-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: Need help in defining relationship in a different tables
PostPosted: Mon Jul 13, 2009 2:29 am 
Newbie

Joined: Mon Jul 13, 2009 1:54 am
Posts: 2
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 ?


Top
 Profile  
 
 Post subject: Re: Need help in defining relationship in a different tables
PostPosted: Tue Jul 14, 2009 5:26 am 
Newbie

Joined: Mon Jul 13, 2009 1:54 am
Posts: 2
Can some one please comment on this ?


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.