hibernate中的对象的3种状态的理解及导致报错object references an unsaved transient instance - save the transient instance before flushing异常
先看下这三篇博文:
object references an unsaved transient instance - save the transient instance before flushing异常问题处理
hibernate中持久化对象的生命周期(三态:自由态,持久态,游离态 之间的转换)
hibernate中对象的3种状态:瞬时态(Transient)、 持久态(Persistent)、脱管态(Detached)
原来的导致报错的代码:
/**
* 详细信息UI
*/
public String showUI() {
if (!StringUtils.isBlank(deviceId)) {
device = deviceService.queryById(deviceId);
}
String deviceTypeName = null;
String deviceTypeNameString ="未知类型";
if(null!= device.getDeviceType()){
deviceTypeName = device.getDeviceType().getName();
deviceTypeNameString = MessageUtils.getMessage(deviceTypeName);
}
DeviceType deviceType = new DeviceType();
deviceType.setName(deviceTypeNameString);
device.setDeviceType(deviceType);
if (device != null && device.getDeviceInfos().iterator().hasNext())
deviceInfo = device.getDeviceInfos().iterator().next();
String osNameString ="未知操作系统类型";
if(null != device.getOs()){
if(StringUtils.isNotBlank(device.getOs().getName())){
String osName = device.getOs().getName();
osNameString = MessageUtils.getMessage(osName);
}
}else{
device.setOs(new Os());
}
device.getOs().setName(osNameString);
String ufNameString ="未知设备用途";
if(null!=device.getDeviceUseful()){
if( StringUtils.isNotBlank(device.getDeviceUseful().getName())){
String ufName= device.getDeviceUseful().getName();
ufNameString = MessageUtils.getMessage(ufName);
}
}else{
device.setDeviceUseful(new DeviceUseful());
}
device.getDeviceUseful().setName(ufNameString);
//获取组织机构全路径
Organization organization=device.getOrganization();
if(organization!=null){
String Aname =organization.getName();
String name= getAname(organization, Aname);
device.getOrganization().setName(name);
}
return RETURN_SHOWUI;
}
错误点就在上面代码标红处,估计他这么写的意思就是像把国际化的名称转为汉语,但是这样写就会导致deviceType类中只有name是有值的,其他都是null,所以会报错:org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing:
考虑可以修改成这样:
public String showUI() {
if (!StringUtils.isBlank(deviceId)) {
device = deviceService.queryById(deviceId);
}
String deviceTypeName = null;
String deviceTypeNameString ="未知类型";
if(null!= device.getDeviceType()){
deviceTypeName = device.getDeviceType().getName();
deviceTypeNameString = MessageUtils.getMessage(deviceTypeName);
device.getDeviceType().setName(deviceTypeNameString);
}else{
device.setDeviceType(new DeviceType());
}
if (device != null && device.getDeviceInfos().iterator().hasNext())
deviceInfo = device.getDeviceInfos().iterator().next();
String osNameString ="未知操作系统类型";
if(null != device.getOs()){
if(StringUtils.isNotBlank(device.getOs().getName())){
String osName = device.getOs().getName();
osNameString = MessageUtils.getMessage(osName);
device.getOs().setName(osNameString);
}
}else{
device.setOs(new Os());
}
String ufNameString ="未知设备用途";
if(null!=device.getDeviceUseful()){
if( StringUtils.isNotBlank(device.getDeviceUseful().getName())){
String ufName= device.getDeviceUseful().getName();
ufNameString = MessageUtils.getMessage(ufName);
device.getDeviceUseful().setName(ufNameString);
}
}else{
device.setDeviceUseful(new DeviceUseful());
}
//获取组织机构全路径
Organization organization=device.getOrganization();
if(organization!=null){
String Aname =organization.getName();
String name= getAname(organization, Aname);
device.getOrganization().setName(name);
}
return RETURN_SHOWUI;
}
这样修改最重要的点就是上面标红处,但是依然有问题,就是当device.getDeviceUseful()等是null的时候,会报上面的异常,还是自由态的影响,所以考虑到放到session里面,到详情页面再直接取即可。
public String showUI() {
if (!StringUtils.isBlank(deviceId)) {
device = deviceService.queryById(deviceId);
}
String deviceTypeName ="未知类型";
if(null!= device.getDeviceType()){
deviceTypeName = MessageUtils.getMessage(device.getDeviceType().getName());
}
getRequest().setAttribute("deviceTypeName", deviceTypeName);
if (device != null && device.getDeviceInfos().iterator().hasNext())
deviceInfo = device.getDeviceInfos().iterator().next();
String osName ="未知操作系统类型";
if(null != device.getOs()){
if(StringUtils.isNotBlank(device.getOs().getName())){
osName = MessageUtils.getMessage(device.getOs().getName());
}
}
getRequest().setAttribute("osName", osName);
String ufName ="未知设备用途";
if(null!=device.getDeviceUseful()){
if(StringUtils.isNotBlank(device.getDeviceUseful().getName())){
ufName = MessageUtils.getMessage(device.getDeviceUseful().getName());
}
}
getRequest().setAttribute("ufName", ufName);
//获取组织机构全路径
Organization organization=device.getOrganization();
if(organization!=null){
String Aname =organization.getName();
String name= getAname(organization, Aname);
device.getOrganization().setName(name);
}
return RETURN_SHOWUI;
}