-->
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.  [ 7 posts ] 
Author Message
 Post subject: Issue with One to many mapping
PostPosted: Tue Feb 22, 2011 3:22 am 
Newbie

Joined: Mon Jan 31, 2011 2:24 pm
Posts: 6
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.

Regards,
Ben


Top
 Profile  
 
 Post subject: Re: Issue with One to many mapping
PostPosted: Tue Feb 22, 2011 6:39 am 
Regular
Regular

Joined: Fri Jan 28, 2011 11:44 am
Posts: 117
Hi,

One-To-Many is the right way to do it.
Can you post your mapping to see what's wrong?


Top
 Profile  
 
 Post subject: Re: Issue with One to many mapping
PostPosted: Wed Feb 23, 2011 4:08 am 
Newbie

Joined: Mon Jan 31, 2011 2:24 pm
Posts: 6
Hi,

Thanks a lot for your response. My "Division" mapping is below -

Code:
<class name="Division" table="DIVISION">
      <id name="divisionId" type="java.lang.Long" column="DIVISION_ID"/>
      
      <property name="divisionName" type="java.lang.String"
      column="DIVISION_NM" length="50" />

      <set name="employees" cascade="all" inverse="false">
         <key column="DIVISION_ID" />
         <one-to-many class="Employees" />
</class>

public class Division implements Serializable {

   long divisionId;
   String divisionName;
   public Set employees;
   
   getter & setters...
}


The issue I have is when Division is inserted; I dont have the Set of "employees". In my scenario, I need to first insert the Division and at a later point in time, my batch program fetches the Employees objects and this should be inserted into Oracle by Hibernate. But when employees is inserted, need to make sure that Division_Id already exists in the Division table.

Will this scenario work using One to Many or is there any other way out? Please let me know. Thanks.


Top
 Profile  
 
 Post subject: Re: Issue with One to many mapping
PostPosted: Wed Feb 23, 2011 6:02 am 
Regular
Regular

Joined: Fri Jan 28, 2011 11:44 am
Posts: 117
Hi,

2 more questions before being able to answer:
How is mapped the Employees class, is the relation bidirectionnal?
When you insert the employees do you only save the employees or do you save the whole division?


Top
 Profile  
 
 Post subject: Re: Issue with One to many mapping
PostPosted: Wed Feb 23, 2011 5:17 pm 
Newbie

Joined: Mon Jan 31, 2011 2:24 pm
Posts: 6
Hi,

THe relation is unidirectional. And also, when I insert Employees, I will save only the Employees. The Divisions would have been inserted before.
I need to ensure that if the Division_Id in Employees is not existing in the Division table, hibernate should not allow to insert that row in the Employee table. In below example, hibernate should not allow to insert 3rd row in the Employee table as DivisionID 3 does not exist in Division table.

Division table:
DivisionId DivisionName
1 Div1
2 Div2

Employee table:
EmpId EmpName DivisionID
10 Joe 1
20 Ben 2
30 John 3

Thanks a lot..


Top
 Profile  
 
 Post subject: Re: Issue with One to many mapping
PostPosted: Thu Feb 24, 2011 2:18 am 
Newbie

Joined: Mon Jan 31, 2011 2:24 pm
Posts: 6
Hi,

I got this scenario working using the "Many to One" mapping. However, Can you please let me know if my approach is correct? or is there a better way to do this?

Code:
<class name="Division" table="DIVISION">
      <id name="divisionId" type="java.lang.Long" column="DIVISION_ID"/>     
      <property name="divisionName" type="java.lang.String" column="DIVISION_NM" length="50" />
</class>

<class name="Employee" table="EMPLY">
      <id name="empId" column="EMPID" type="java.lang.Long"> </id>
      <property name="empName" type="java.lang.String" column="EMPNAME" length="100" />
      
                <many-to-one name="division" column="divisionId" />
</class>

//Java section
session.saveOrUpdate(Division1);
session.saveOrUpdate(Division2);
:
:
Employee e1 = new Employee();
         e1.setEmpId(10);
    e1.setName("Joe");       
    e1.setDivision(Division1);
session.saveOrUpdate(e1);       //executes successfully

Employee e2 = new Employee();
         e2.setEmpId(11);
    e2.setName("Ben");       
    e2.setDivision(Division1);
session.saveOrUpdate(e2);        //executes successfully

Employee e3 = new Employee();
         e2.setEmpId(12);
    e3.setName("John");       
    e3.setDivision(Division2);
session.saveOrUpdate(e3);        //executes successfully

Employee e3 = new Employee();
         e2.setEmpId(13);
    e3.setName("John");       
    e3.setDivision(Division3);
session.saveOrUpdate(e3);         //Does not execute as Division3 does not exist in DB



Top
 Profile  
 
 Post subject: Re: Issue with One to many mapping
PostPosted: Thu Feb 24, 2011 5:43 am 
Regular
Regular

Joined: Fri Jan 28, 2011 11:44 am
Posts: 117
Hi,

If your relation is unidirectionnal from Division to Employee then its normal that saving only the Employe in the second stage of your migration won't work ...

There are different ways to implement what you are looking for:

1. Unidirectionnal Mapping:
a. Many-To-One in the Employee Class mapping and saving directly the Employees (Problem a Division doesn't know the list of its Employees!)
b. One-To-Many inverse=false cascade=all in the Division Class mapping and updating the Division (Problem an Employee doesn't know its Division)
c. One-To-Many inverse=true in the Division Class mapping and saving directly the Employees (Problem an Employee doesn't know its Division)

2. Bidirectionnal Mapping: (a Division will know its Employees and an Employee will know its Division)
a. One-To-Many inverse=false cascade=all in the Division Class mapping, Many-To-One in the Employee Class mapping. In this case you can either update the Division or save directly the Employees but the generated SQL might not be optimized ...
b. One-To-Many inverse=true in the Division Class mapping, Many-To-One in the Employee Class mapping and saving directly the Employees


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 7 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.