• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

one to many concepts

 
Ranch Hand
Posts: 122
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Men call me Jim. Women look past me to this tiny ad:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic