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.xmlCode:
<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.