zoukankan      html  css  js  c++  java
  • 关于hibernate中映射中有many to one等外键关联时的问题

      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;
    }
  • 相关阅读:
    【转】ERROR: Cannot uninstall 'llvmlite'. It is a distutils installed project. 此类报错的解决办法
    【转】python setup.py install 报错:error:[WinError 3]系统找不到指定的路径: 'C:\\Program Files (x86)\\Microsoft Visual Studio 14.0\\VC\\PlatformSDK\\lib
    【转】距离相关系数以及python包的安装
    【转】算法总结这是一份全面并且详细的排序算法学习指南
    origin 一键导出文件夹中所有图形
    【转】距离相关系数的python实现
    [转]一文让你通俗理解奇异值分解
    origin 检查是否有重复图片
    【Vegas原创】centos网卡自启动
    【Vegas原创】使用dockercompose运行mysql8
  • 原文地址:https://www.cnblogs.com/goloving/p/8268849.html
Copyright © 2011-2022 走看看