• 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
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Devaka Cooray
  • Ron McLeod
  • paul wheaton
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Frits Walraven
  • Scott Selikoff

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
 
No matter. Try again. Fail again. Fail better. This time, do it with this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic