关于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;
}
关于hibernate中映射中有many to one等外键关联时的问题的更多相关文章
- T-SQL中找出一个表的所有外键关联表
二种方法(下例中表名为T_Work) 1.SQL查询系统表 SELECT 主键列ID=b.rkey ,主键列名=(SELECT name FROM syscolumns WHERE colid=b.r ...
- hibernate5(12)注解映射[4]一对一外键关联
在实际博客站点中,文章内容的数据量非常多,它会影响我们检索文章其他数据的时间,如查询公布时间.标题.类别的等. 这个时候,我们能够尝试将文章内容存在还有一张表中,然后建立起文章--文章内容的一对一映射 ...
- Hibernate中映射一对一关联(按主键映射和外键映射)和组件映射
Hibernate中映射一对一关联(按主键映射和外键映射)和组件映射 Hibernate提供了两 ...
- Hibernate,关系映射的多对一单向关联、多对一双向关联、一对一主键关联、一对一外键关联、多对多关系关联
2018-11-10 22:27:02开始写 下图内容ORM.Hibernate介绍.hibername.cfg.xml结构: 下图内容hibernate映射文件结构介绍 下图内容hibernate ...
- Hibernate 再接触 关系映射 一对一单向外键关联
对象之间的关系 数据库之间的关系只有外键 注意说关系的时候一定要反面也要说通 CRUD 数据库之间设计 主键关联 单向的外键关联 中间表 一对一单向外键关联 Husband.java package ...
- Hibernate关系映射 一对一双向外键关联@OneToOne Annotation方式 双向关联和单向关联的区别
首先还是来构造一个实际应用的场景,比如实体类车辆(Car),它具有以下属性:Id,品牌(brand),车牌(lisencePlate):实体类车牌(LisencePlate),它具有以下属性:Id,号 ...
- Hibernate关系映射 一对一双向外键关联@OneToOne Annotation方式
首先还是来构造一个实际应用的场景,比如实体类车辆(Car),它具有以下属性:Id,品牌(brand),车牌(lisencePlate):实体类车牌(LisencePlate),它具有以下属性:Id,号 ...
- hibernate 关系映射之 单向外键关联一对一
这里的关系指的是对象与对象之间的关系 注解方式单向关联一对一: //这个类描述的husband是一个对应一个wife的 import javax.persistence.Entity; import ...
- hibernate一对一双向外键关联
一对一双向外键关联:双方都持有对方的外键关联关系. 主控方和一对一单向外键关联的情况是一样的,主要的差异表现为,被空方需要添加: @OneToOne(mappedBy="card" ...
随机推荐
- CodeForces 549H | 二分答案
参考了这个博客哇 #include<cstdio> #include<algorithm> #include<cstring> #define Max(a,b,c, ...
- 自动设置 rem es模块写法
export default function () { let html = document.documentElement; function onWindowResize() { if (ht ...
- Windows.Forms Panel 动态加载用户控件 UserControl
创建好一个Windows Forms程序,在创建好的程序中Form1添加一个Panel控件 如图:
- Html5学习进阶一 视频和音频
HTML5 规定了一种通过 video 元素来包含视频的标准方法. 视频格式 当前,video 元素支持两种视频格式: Internet Explorer Firefox 3.5 Opera 10 ...
- flexigrid 学习总结
最近看到了一款flexigrid表格组件,简单美观,在下载使用的过程中,发现缺少很多功能.<基于jQuery的GridView-Flexigrid(2)-扩展和修复>给我带来了很大的帮助, ...
- 放棋游戏(NOIP模拟赛)(DP)
没有原题... 囧.. [问题描述] 游戏规则是这样,有n(1<=n<=100)行格子,第一行由n个格子,第二行有n-1个格子,第三行由n-2个格子,……以此类推,第n行有1个格子.要求再 ...
- 斗地主(NOIP2015)
原题传送门 神奇的题目.. 一开始我准备打暴力直接搜答案. 然后发现.. 无限TLE.. 因为O((logN)^14*T)BOOM.. 然后Zxyer告诉可以只DFS顺子...其他的可以一步搞出来.. ...
- linux用户态定时器的使用---19【转】
转自:http://www.cnblogs.com/zxouxuewei/p/5095288.html 原创博文,转载请标明出处--周学伟http://www.cnblogs.com/zxouxuew ...
- V4L2 camera 驱动 capture测试程序【转】
转自:http://blog.csdn.net/kickxxx/article/details/6336346 在网上找了一个测试程序, 看了看,是根据capture.c修改的.测试步骤如下 . gc ...
- [转]Google 的开源技术protobuf 简介与例子
本文来自CSDN博客:http://blog.csdn.NET/program_think/archive/2009/05/31/4229773.aspx 今天来介绍一下“Protocol Buffe ...