Hello Friends,
I have got a parent/child one-to-many mapping 'Employee to ServiceEntry'. My domains and mapping files has been listed
below.
I just want to know whether the adding and deleting part of
child given below in ServiceEntryService.java is correct?
ServiceEntry.java
--------------------
public class ServiceEntry
{
private Long id;
.......
private Employee attendedBy;
geters......
seters......
public Employee getAttendedBy()
{
return attendedBy;
}
public void setAttendedBy(Employee attendedBy)
{
this.attendedBy = (Employee)attendedBy;
}
public Employee getCurrentlyAssignedTo()
{
return currentlyAssignedTo;
}
}
Employee.java
-----------------
public class Employee
{
private Integer id;
.......
private Set serviceCallAttendedList = new HashSet();
geters......
seters......
public Set getServiceCallAttendedList()
{
return serviceCallAttendedList;
}
public void setServiceCallAttendedList(Set serviceCallAttendedList)
{
this.serviceCallAttendedList = serviceCallAttendedList;
}
public void addServiceCallAttendedEntry(ServiceEntry aServiceEntry)
{
if ( serviceCallAttendedList == null )
{
serviceCallAttendedList = new HashSet();
}
serviceCallAttendedList.add(aServiceEntry);
}
public void removeServiceCallAttendedEntry(ServiceEntry aServiceEntry)
{
serviceCallAttendedList.remove(aServiceEntry);
}
}
ServiceEntry.hbm.xml
--------------------------
..........
<hibernate-mapping>
<class name="com.sample.domain.ServiceEntry" table="service_entry">
<id name="id" column="id">
<generator class="native" />
</id>
...........
...........
<many-to-one name="attendedBy" class="com.sample.domain.Employee" column="attendedBy" not-null="true" />
</class>
</hibernate-mapping>
Employee.hbm.xml
----------------------
..........
<hibernate-mapping>
<class name="com.sample.domain.Employee" table="employee">
<id name="id" type="integer" column="id">
<generator class="native" />
</id>
...........
...........
</set>
<set name="serviceCallAttendedList" inverse="true" lazy="true" table="service_entry">
<key column="attendedBy" not-null="true" />
<one-to-many class="com.sample.domain.ServiceEntry" />
</class>
</hibernate-mapping>
ServiceEntryService.java
-----------------------------
public class ServiceEntryService implements IServiceEntryService
{
..........
..........
@Transactional(propagation = Propagation.REQUIRED)
public void saveServiceEntry(ServiceEntry serviceEntry, Integer employeeId)
{
try
{
Employee employeeAttended = employeeDAO.loadEmployee(employeeId);
serviceEntry.setAttendedBy(employeeAttended);
employeeAttended.addServiceCallAttendedEntry(serviceEntry);
serviceEntryDAO.saveServiceEntry(serviceEntry);
}
catch (Exception exception)
{
throw new BusinessEntityException(exception);
}
}
@Transactional(propagation = Propagation.REQUIRED)
public void deleteServiceEntry(ServiceEntry serviceEntry)
{
serviceEntry.getAttendedBy().getServiceAssignedList().remove(serviceEntry);
serviceEntryDAO.deleteServiceEntry(serviceEntry);
}
}
Thank you.
Sudheer
|