org.unsaved transient instance - save the transient instance before flushing: bug解决方案
最近开发和学习过程中,遇到很多零碎的知识点,在此简单地记录下:
1.遇如下bug:
org.unsaved transient instance - save the transient instance before flushing: org.blog.blog.domain.HighStudent
解释:意思是说hibernate从数据库中查询出来的一个对象HighSchool(为持态,我给它的事物定义为readOnly,只有只读权限),然后又给它的属性或者它属性的属性(highStudents.comment.cxpy)进行set值,这样hibernate是不允许的。因为你对一个事物为只读状态的对象进行赋值,显然是不允许,除非你主动调用saveOrUpate
方法。serice方法事物是readOnly,dao中就查询出来的对象不允许修改属性。
解决方法:将查询出来的对像使用session.evict()方法转换为游离态,此时就可以查询成功。代码如下:
@Override
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
public List<PrimaryStudents> getPrimaryStudentsList(String[] stuids)
throws StuBaseDataException {
List<PrimaryStudents> list=new ArrayList<PrimaryStudents>();
String stu=stuids[0];
String [] studentids=stu.split(",");
System.out.println(studentids.length);
try{
if(studentids.length>0){
for(int i=0;i<studentids.length ;i++){
PrimaryStudents primaryStudents=null;
primaryStudents=this.primaryStudentsDao.queryPrimaryStudentsBystuid(studentids[i]);
if(primaryStudents==null){
throw new StuBaseDataException("该批学生中存在学生信息不存在的学生,请刷新之后再进行该操作");
}
list.add(primaryStudents);
}
}else{
throw new StuBaseDataException("请选择一位学生");
}
}catch (Exception e) {
e.printStackTrace();
throw new StuBaseDataException("查询失败!!!",e);
}
return list;
}
@Override
public PrimaryStudents queryPrimaryStudentsBystuid(String stuid) {
List<PrimaryStudents> list= null;
Session session = this.getHibernateTemplate().getSessionFactory().getCurrentSession();
String sql="From PrimaryStudents ps where ps.id in("+stuid+")";
Query query = session.createQuery(sql);
list = query.list();
if (list != null && list.size() > 0)
{
/**
* 在做学籍卡批量打印的时候,显示问题。
* 评语不足6个的时候,构造几个空的,显示到页面上去。
* 强制转换为游离态
*/
session.evict(list.get(0));
return (PrimaryStudents)list.get(0);
}
return null;
}
org.unsaved transient instance - save the transient instance before flushing: bug解决方案的更多相关文章
- ERROR org.hibernate.internal.SessionImpl - HHH000346: Error during managed flush [object references an unsaved transient instance - save the transient instance before flushing: cn.itcast.domain.Custom
本片博文整理关于Hibernate中级联策略cascade和它导致的异常: Exception in thread "main" org.hibernate.TransientOb ...
- object references an unsaved transient instance save the transient instance before flushing
object references an unsaved transient instance save the transient instance before flushing 对象引用未保存的 ...
- object references an unsaved transient instance - save the transient instance before flushing: com.jspxcms.core.domain.ScTeam
object references an unsaved transient instance - save the transient instance before flushing: com.j ...
- object references an unsaved transient instance - save the transient instance before flushing异常问题处理
一.异常:org.hibernate.TransientObjectException: object references an unsaved transient instance - save ...
- object references an unsaved transient instance - save the transient instance before flushing错误
异常1:not-null property references a null or transient value解决方法:将“一对多”关系中的“一”方,not-null设置为false(参考资料: ...
- hibernate 对象状态异常:object references an unsaved transient instance - save the transient instance before flushing
我的问题出在,删除的对象对应的表中有一个外键,关联着另外一个表,可是另外一个表中没有数据,所以报了这个错误. 参考http://www.cnblogs.com/onlywujun/archive/20 ...
- Hibernate的一个问题object references an unsaved transient instance - save the transi5
1 我做了一对多和多对一的双向关联关系,在User这一方@ManyToOne已经设置了级联Cascade,这样在测试程序中保存User时,Group也应该自动保存,为什么会抛出以下的异常: (我是按着 ...
- save the transient instance before flushing错误解决办法 【待完善】
近日在项目中遇到以下错误,着实郁闷了一把: org.hibernate.TransientObjectException: object references an unsaved transient ...
- 【总文档】rac增加新节点的方法步骤 How to Add Node/Instance or Remove Node/Instance in 10gR2, 11gR1, 11gR2 and 12c Oracle Clusterware and RAC
[总文档]How to Add Node/Instance or Remove Node/Instance in 10gR2, 11gR1, 11gR2 and 12c Oracle Clusterw ...
随机推荐
- MyBatis学习总结_02_使用MyBatis对表执行CRUD操作
一.使用MyBatis对表执行CRUD操作——基于XML的实现 1.定义sql映射xml文件 userMapper.xml文件的内容如下: 1 <?xml version="1.0&q ...
- iOS:核心动画之动画组CAAnimationGroup
CAAnimationGroup——动画组 动画组,是CAAnimation的子类,可以保存一组动画对象,将CAAnimationGroup对象加入层后,组中所有动画对象可以同时并发运行 属性说明: ...
- Spring boot 整合jsp和tiles模板
首先贴上我的pox.xml文件,有详细的支持注释说明 <?xml version="1.0" encoding="UTF-8"?> <proj ...
- java基础篇---I/O技术
java基础篇---I/O技术 对于任何程序设计语言而言,输入输出(I/O)系统都是比较复杂的而且还是比较核心的.在java.io.包中提供了相关的API. java中流的概念划分 流的方向: 输 ...
- 基于EntityFramework的权限的配置和验证
1. 概要 本文主要介绍公司现有系统框架的权限体系,和一些待扩展功能的说明.目前该权限体系基于角色构建(RBAC),原则上,系统中不允许出现对用户.组织等其他对象指派权限的情况. 2. 权限分 ...
- POJ2282:The Counting Problem(数位DP)
Description Given two integers a and b, we write the numbers between a and b, inclusive, in a list. ...
- python中的列表(list) 切片详解
1.切片: 通过指定下标的方式来获得某一个数据元素,或者通过指定下标范围来获得一组序列的元素,这种访问序列的方式叫做切片. 访问某一个数据元素的的语法如下: sequence[index] ...
- WINCE设备开机灰屏问题(很怪异)
WINCE设备开机灰屏问题(很怪异) 1. 问题现象 图1 无法进入系统,虽然没有调试信息,但应该可以判断是程序跑飞了.我们这款产品用到3种显示屏(采用不同的驱动IC),可是测试发现1和2号屏 ...
- non-overlapping-intervals
https://leetcode.com/problems/non-overlapping-intervals/ 其中还用到了Java的Comparator接口和其中的compare方法. packa ...
- POI刷新数据后的函数(公式)更新问题
使用POI将Excel模板中的数据进行更新,这应该是很常见的操作 下面就贴上我的一小段代码 public class ModifyExcel { /** * @param fileName Excel ...