Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Object Relational Mapping and the fly likes one to many concepts Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "one to many concepts" Watch "one to many concepts" New topic
Author

one to many concepts

visu Nekk
Ranch Hand

Joined: Sep 06, 2005
Posts: 122
Hi,

I am a newbie to hibernate and am struck with the on-to-many usage. Consider this scenario: I have two tables department and employee and the relation between them is one-to-many.

Department table:
department_id int primary key
department_name not null varchar(100)

Employee table:
employee_id int primary key
employee_name not nul varchar(100)
department_id int foreign key references department(department_id)

The data in the Department table is pre-defined as there will be fixed number of departments in any organization.
Now, I need to create new employee and assign them to a department. For this purpose, I created a jsp page with a text box for name and a drop down list pre-populated with departments. To insert the record, it was pretty straightforward. Using request.getParameter(), I retrieved the values of name and department and assigned them to the Employee object. Using the session.save(), I am able to save the object without using the one-to-many relationship and I suppose it has to be done that way only.

From the examples I found on the web for one-to-many relationship, the parent object is saved first and then the child object.

Now my question is : Should we use one-to-many relationship only when we need to store objects in both the tables?

Regards,
Viswanath
 
jQuery in Action, 2nd edition
 
subject: one to many concepts