-->
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.  [ 6 posts ] 
Author Message
 Post subject: Another parent/child problem ...
PostPosted: Tue Mar 20, 2007 5:28 am 
Newbie

Joined: Tue Mar 20, 2007 5:13 am
Posts: 9
Hi, I just started to learn hibernate and I found problem regarding parent/child with hibernate.

I have 2 tables, Employee & Department where [Department] has one to many [Employee]

Department.hbm.xml
Code:
<class name="Department" table="DEPARTMENT">
   
        <id name="departmentId" column="DEPARTMENT_ID" type="integer" unsaved-value="0">
            <generator class="identity"/>
        </id>
       
        <property name="departmentName"   column="DEPARTMENT_NAME"
                    type="string"
                    length="30"
                    not-null="true"/>

        <set name="employee" cascade="all">
           <key column="DEPARTMENT_ID"/>
           <one-to-many class="Employee" />
        </set>

</class>


Employee.hbm.xml
Code:
<class name="Employee" table="EMPLOYEE">
   
        <id name="employeeUserName" column="EMPLOYEE_USERNAME" type="string" length = "50">
            <generator class="assigned"/>
        </id>
       
        <property name="employeeFName"    column="EMPLOYEE_FIRSTNAME"
                    type="string"
                    length="50"/>
       
        <property name="employeeLName"    column="EMPLOYEE_LASTNAME"
                    type="string"
                    length="50"
                    not-null="true"/>
       
        <property name="employeePassword" column="EMPLOYEE_PASSWORD"
                    type="string"
                    length="10"
                    not-null="true"/>
             
</class>


I have an object class for both [Department] and [Employee] and I have no problem adding department and employee to table together by using this following code:

Code:
Employee emp = new Employee("John", "Doe", bla bla);
Department dept = new Department("DeptName");
dept.addEmployee(emp);

session.save(dept);


But later in my program i want to have some ability to let me add employee without accessing Department class. Maybe by doing something like below..

Code:
Employee emp = new Employee("John", "Doe", bla bla);
emp.setDepartmentId(4);

session.save(emp);


If I want to do something like that, How should I update my hibernate xml file?

of if I can't do like what i wish to do, How is the best way to handle this problem without pulling out the Department Object again?

Thanks a lot for any help.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 20, 2007 6:26 am 
Beginner
Beginner

Joined: Thu Mar 01, 2007 9:40 am
Posts: 23
Location: UK
Hi,

You need to have access to the department object to set the employee object. So, the code for inserting an Employee will be as follows:
Code:
Employee emp = new Employee("John", "Doe", bla bla);
emp.setDepartment(departmentObject);
session.save(emp);


Hibernate will pick up the foreign key ID from the department object set on the employee object.

_________________
Sam.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 20, 2007 6:29 am 
Beginner
Beginner

Joined: Thu Mar 01, 2007 9:40 am
Posts: 23
Location: UK
Hi,

You can set the employee by using the parent Department object. Following code can be of help.
Code:
Employee emp = new Employee("John", "Doe", bla bla);
emp.setDepartment(departmentObject);
session.save(emp);

As you can see, we're providing the departmentObject for hibernate to resolve the foreign key relation from employee to department.

_________________
Sam.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 21, 2007 8:37 am 
Newbie

Joined: Tue Mar 20, 2007 5:13 am
Posts: 9
Jikes2005 wrote:
Hi,

You can set the employee by using the parent Department object. Following code can be of help.
Code:
Employee emp = new Employee("John", "Doe", bla bla);
emp.setDepartment(departmentObject);
session.save(emp);

As you can see, we're providing the departmentObject for hibernate to resolve the foreign key relation from employee to department.


Hi Jikes,

That is what I want from my code, but I don't really know what I need to add to my Employee.hbm.xml code to achieve that.

I assume I only need to add these following line to my Employee POJO
Code:
private Set Department = new HashSet()

public Set getDepartment(){ ... }
public void setDepartment(Set dept){ ... }


could anyone please help me on what should I add to my Employee.hbm.xml?

Thanks for any reply.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 21, 2007 8:54 am 
Beginner
Beginner

Joined: Thu Mar 01, 2007 9:40 am
Posts: 23
Location: UK
Hi,

You need to modify your Employee mapping as below:

Code:
<class name="Employee" table="EMPLOYEE">
   
        <id name="employeeUserName" column="EMPLOYEE_USERNAME" type="string" length = "50">
            <generator class="assigned"/>
        </id>
       
        <property name="employeeFName"    column="EMPLOYEE_FIRSTNAME"
                    type="string"
                    length="50"/>
       
        <property name="employeeLName"    column="EMPLOYEE_LASTNAME"
                    type="string"
                    length="50"
                    not-null="true"/>
       
        <property name="employeePassword" column="EMPLOYEE_PASSWORD"
                    type="string"
                    length="10"
                    not-null="true"/>               

        <many-to-one name="department" column="DEPARTMENT_ID" not-null="true" />
</class>


This tells that employee has a many-to-one relationsgip with Department. Your POJO isn't entirely correct. You'r Employee pojo should look like:
Code:
private Department department;
public Department getDepartment(){ return department; }
public void setDepartment(Department dept){ this.department = dept; }


This is so that an employee belongs to one department, while a department will have an HashSet of employees.

_________________
Sam.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 21, 2007 9:25 am 
Newbie

Joined: Tue Mar 20, 2007 5:13 am
Posts: 9
thanks for your help. It works perfectly!

just another note for whoever might face a same problem...

My many-to-one is as follow:

Code:
<many-to-one name="departmentId" column="DEPARTMENT_ID"
                                        class="Department"
                                        not-null="true" />


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