• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Issue with One to many mapping

 
achsu subra
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

I am new to Hibernate. I have 2 tables Division & Employee. A Division can have many Employees. Primary key Division_Id is a foreign key in the Employee table. There is batch that fetches the Division objects first and then fetches the Employee objects later.

I need to insert the Division & Employee objects into another Database for which I am using Hibernate. Division objects are inserted first and later the Employee objects (as Employee objects are queried later in the batch). When I insert the Employee objects, I still need to make sure the Foreign key constraint for Division_Id. That is, when the Employee is inserted, I need to check that the corresponding Division entry exists.

I tried "1 to many" and this works in the case when both Division & Employee data exists at the same time. But in my case, Division is inserted 1st and Employee should be inserted next.

Can anyone please assist what approach needs to be followed for this. Thanks a lot in advance.


 
Emanuel Kadziela
Ranch Hand
Posts: 187
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What you described should work. What happens? What do you mean it does not work?

As long as you make the "division" column in the "Employee" table a non-nullable foreign key, the database should enforce it for you and hibernate will just follow suit. Many To One is the correct association to use for the Employee (many employees, one division).
 
Ganesh Shirsat
Ranch Hand
Posts: 33
Google Web Toolkit Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What you described should work.

Division and Employee Table
is there is one to many relationship in Division mapping file and many to one in Employee mapping file?

have you add the "inverse" attribute in one to many element.

if yes then your approach should.

otherwise,
will you please put the mapping file of both class.?


 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic