Thanks for the info.
saveOrUpdate() method to save subsequent records works fine.
But i have problems with the Update i.e. updating associated objects results in updating of one object and insertion of associated object.
For example
The associated objects in my case are DeviceType and DeviceTypeRoles. Here is the sequence of method calls to update associated objects
In JSF Backing Bean
Code:
public void updateDeviceType(){
deviceType.setId(Long.valueOf(selectedDeviceType));
deviceType.setDeviceTypeRoles(createDeviceTypeRoleSet());
deviceTypeManager.updateDeviceType(deviceType);
this.deleteBtnMode = true;
FacesUtils.addInfoMessage("update", "DeviceType", deviceType.getName());
}
public Set createDeviceTypeRoleSet()
{
//System.out.println("In RoleList method ");
//Iterator it = deviceRoles.iterator();
//List roleList = new ArrayList();
Set deviceTypeRoleSet = new HashSet();
deviceTypeRole = new DeviceTypeRole();
System.out.println("In Backing Bean: Adding Roles To Device Type, RoleSet Size="+getDeviceRoles()+""+deviceRoles);
for (Iterator iter = deviceRoles.iterator(); iter.hasNext();) {
System.out.println("Inside for loop");
SelectItem selectItem = (SelectItem) iter.next();
String roleId = (String)selectItem.getValue();
System.out.println("In createDeviceTypeRoleSet method roleId= "+roleId);
deviceTypeRole = deviceTypeManager.addRoleToDeviceType(Long.valueOf(roleId));
deviceTypeRole.setDeviceType(deviceType);
deviceTypeRoleSet.add(deviceTypeRole);
//roleList.add(selectItem.getValue());
}
System.out.println("In createDeviceTypeRoleSet method Set size="+deviceTypeRoleSet.size());
//return new HashSet(roleList);
return deviceTypeRoleSet;
}
In Spring Bean
Code:
public void updateDeviceType(DeviceType deviceType){
logger.info(" *** In updateDeviceType Manager method *** ");
System.out.println(" *** In updateDeviceType Manager method *** ");
deviceType.setNamsUser(baseManager.getNamsUserByChangeUserId());
deviceType.setChangeDate(new Date());
deviceTypeDao.update(deviceType);
}
public DeviceTypeRole addRoleToDeviceType(Long roleId){
System.out.println("In addRoleToDeviceType method");
DeviceTypeRole deviceTypeRole = new DeviceTypeRole();
try{
NamsRole namsRole = namsRoleDao.get(roleId);
System.out.println("RoleName in addRoleToDeviceType="+namsRole.getName());
deviceTypeRole.setNamsRole(namsRole);
deviceTypeRole.setNamsUser(baseManager.getNamsUserByChangeUserId());
deviceTypeRole.setChangeDate(new Date());
}catch(Exception e){
logger.error(e.getMessage());
}
return deviceTypeRole;
}
In HibernateImpl
Code:
public void update( DeviceType object) {
hibernateTemplate.update(object);
}
Hibernate Mapping File
Code:
<set name="deviceTypeRoles" inverse="true" cascade="save-update">
<key>
<column name="DEV_TYPE_ID" precision="10" scale="0" not-null="true" />
</key>
<one-to-many class="com.boeing.nmt.nams.model.DeviceTypeRole" />
</set>
Hibernate Named Queries
Code:
<query name="findDeviceTypes">
<![CDATA[
from com.xxx.model.DeviceType dt order by dt.name
]]>
</query>
<query name="findDeviceTypeRoles">
<![CDATA[
from com.xx.model.DeviceTypeRole dtr
left join fetch dtr.namsRole
left join fetch dtr.deviceType
where dtr.deviceType.id = :deviceTypeId
]]>
</query>
I do have cascade="save-update" defined in mapping file.
Whenever i update not sure why associated object is doing insert instead of update . Here is the log
Code:
Hibernate: insert into DEVICE_TYPE_ROLE (CHANGE_USER, ROLE_ID, DEV_TYPE_ID, CHANGE_DATE, ID) values (?, ?, ?, ?, ?)
Hibernate: update DEVICE_TYPE set CHANGE_USER=?, NAME=?, DESCRIPTION=?, CHANGE_DATE=? where ID=?
Any pointers/suggestions will be highly appreciated
Regards
Bansi