Im looking for a proper or elegant way to handle updating/deleting/inserting child records when modifying the parent object. For example, I have an employee object that has a list of phone number objects. When a user goes to say an employee edit screen, I need to be able to allow the user to update/delete/save new phone numbers by presenting them a few phone number fields (ex. allowing them to clear an existing phone number which should trigger a delete of that child record)
Currently, this is what Im doing (Im doing this is struts but I dont think that matters):
1) Retrieve the form data
2) Retrieve the employee id and do a hibernate find
3) Determine what phone numbers the user has tried to delete. If user is trying to delete, then remove the phone number from list
4) Determine what phone numbers the user is trying to add or update. If its a new phone number then add to phone number list.
5) Populate an employee object and do an update (ex. session.update(employee))
Maybe Im just expecting hibernate to do all the work for me, like determine that the phone number needs to be deleted when it doesnt find a phone number from the form. Any articles or feedback on how to do a regular scenario like mine would be appreciated.
Hibernate version:
2.1
Mapping documents:
This is the employee.hbm.xml:
<hibernate-mapping package="com.tu.employee.domain">
<class name="Employee" table="EMPLOYEE">
<id name="employeeId" column="EMPLOYEE_ID" type="java.lang.Long" unsaved-value="null">
<generator class="sequence">
<param name="sequence">EMPLOYEE_SEQ</param>
</generator>
</id>
<list name="phoneNumbers" cascade="all-delete-orphan">
<key column="EMPLOYEE_ID"/>
<index column="IDX"/>
<one-to-many class="Phone" />
</list>
<property name="title" column="TITLE" type="java.lang.String" />
<property name="firstName" column="FIRST_NAME" type="java.lang.String" />
<property name="lastName" column="LAST_NAME" type="java.lang.String" />
<property name="createdDate" column="CREATED_DATE" type="java.util.Date" />
<property name="updatedDate" column="UPDATED_DATE" type="java.util.Date" />
<many-to-one name="company" column="COMPANY_ID" class="Company" />
</class>
</hibernate-mapping>
Here is the phone.hbm.xml:
<hibernate-mapping package="com.tu.employee.domain">
<class name="Phone" table="PHONE">
<id name="phoneId" column="PHONE_ID" type="java.lang.Long" unsaved-value="-1" >
<generator class="sequence">
<param name="sequence">PHONE_SEQ</param>
</generator>
</id>
<property name="phoneNumber" column="PHONE_NUMBER" type="java.lang.Long" />
<property name="phoneType" column="PHONE_TYPE" type="java.lang.String" />
<property name="createdDate" column="CREATED_DATE" type="java.util.Date" />
<property name="updatedDate" column="UPDATED_DATE" type="java.util.Date" />
<property name="idx" column="IDX" type="java.lang.Long" />
<many-to-one name="company" column="COMPANY_ID" class="Company" />
<many-to-one name="department" column="DEPARTMENT_ID" class="Department" />
<many-to-one name="employee" column="EMPLOYEE_ID" class="Employee" />
</class>
</hibernate-mapping>
|