jpa删除根据对象删除失败,报Removing a detached instance 错
引用:https://blog.csdn.net/zhanggnol/article/details/6307936
常用数据库表的删除办法,一般都会在DAO类中提供delete.如下例:
public class UnitDAO implements IUnitDAO {
private EntityManager entityManager;
@PersistenceContext
public void setEntityManager(EntityManager entityManager) {
this.entityManager = entityManager;
}
private EntityManager getEntityManager() {
return this.entityManager;
}
public void delete(Unit persistentInstance) {
try {
getEntityManager().remove(persistentInstance);
} catch (RuntimeException re) {
throw re;
}
}
public List<Unit> findAll() {
try {
String queryString = "select model from Unit model";
return getEntityManager().createQuery(queryString).getResultList();
} catch (RuntimeException re) {
throw re;
}
}
}
看上去,没有任何问题,这也是在MyEclipse中自动产生的代码。但是,在实际运行过程中,界面会调用findAll()获得全部的unit记录显示在界面层,根据业务需求,用户选择一个unit进行删除,当调用delete方法时,出现异常:
java.lang.IllegalArgumentException: Removing a detached instance com.gotop.rbac.model.Unit#1
意思就是说,在删除一个detached instance出错。
- 解决办法:
看看Hibernate是如何处理对象的.Chapter 10. Working with objects. http://www.hibernate.org/hib_docs/reference/en/html/objectstate.html
说的很清楚。Hibernate object states有三种状态:Transient、Persistent、Detached。关于Detached,是这么说的:
Detached - a detached instance is an object that has been persistent, but its Session has been closed. The reference to the object is still valid, of course, and the detached instance might even be modified in this state. A detached instance can be reattached to a new Session at a later point in time, making it (and all the modifications) persistent again.
一个detached instance是一个已经持久化的对象,但是它的Session已经关闭了,它的引用依然有效,当然,detached instance可能被修改。detached instance能够在以后可以重新附属到一个新的Session,使之能重新序列化。
好了,找到解决办法了,在删除之前把这个Detached instance绑定到当前的Sesssion,在用当前Sesssion删除此instance。getEntityManager()提供merge方法实现。
修改后的delete代码:
public void delete(Unit persistentInstance) {
try {
getEntityManager().remove(getEntityManager().merge(persistentInstance));
} catch (RuntimeException re) {
throw re;
}
}
千万不要写成:
try {
getEntityManager().merge(persistentInstance);
getEntityManager().remove(persistentInstance);
}
执行完merge后persistentInstance还是detached, merge后返回的新对象才是允许删除的。
jpa删除根据对象删除失败,报Removing a detached instance 错的更多相关文章
- c#封装DBHelper类 c# 图片加水印 (摘)C#生成随机数的三种方法 使用LINQ、Lambda 表达式 、委托快速比较两个集合,找出需要新增、修改、删除的对象 c# 制作正方形图片 JavaScript 事件循环及异步原理(完全指北)
c#封装DBHelper类 public enum EffentNextType { /// <summary> /// 对其他语句无任何影响 /// </summary> ...
- hibernate有关联关系删除子表时可能会报错,可以用个clear避免错误
//清除子表数据 public SalesSet removeSalesSetDistributor(SalesSet salesSet ){ List<SalesSetDistributor& ...
- C++:如何删除string对象的末尾非数字字符
功能实现: 现有一个string对象包含数字字符以及非数字字符,实现删除string对象的末尾非数字字符. 实例: 输入为"0 1 1 2 3 " 输出为"0 1 ...
- 删除JavaScript对象中的元素
参考http://stackoverflow.com/questions/208105/how-to-remove-a-property-from-a-javascript-object 通过dojo ...
- Linux删除ORACLE数据库用户失败提示ORA-01940解决方法
操作环境 SuSE11+Oracle11gR2 问题现象 删除ORACLE数据库用户失败,提示ORA-01940: cannot drop a user that is currently conne ...
- 使用LINQ、Lambda 表达式 、委托快速比较两个集合,找出需要新增、修改、删除的对象
本文需要对C#里的LINQ.Lambda 表达式 .委托有一定了解. 在工作中,经常遇到需要对比两个集合的场景,如: 页面集合数据修改,需要保存到数据库 全量同步上游数据到本系统数据库 在这些场景中, ...
- 创建/读取/删除Session对象
//创建Session对象 Session["userName"] = "顾德博";//保存,这里可以存储任意类型的数据,包括对象.集合等 Session.Ti ...
- unity编辑器扩展_05(删除游戏对象并具有撤回功能)
代码: [MenuItem("Tools/Delete",false,1)] static void Delete() { GameObject[] go ...
- 使用thinkPHP框架实现删除和批量删除一例【原创】
本文为作者原创,转载请注明原作者及转载地址. 上一篇讲了如何用thinkPHP框架实现数据的添加,那这一篇就讲一下如何用thinkPHP实现数据的删除和批量删除吧. 预期效果图: 原谅博主对照片的处理 ...
随机推荐
- vue的使用配置
我的编辑器是webstorm,虽然占内容占资源, 但是用起来很方便, 刚开始接触的时候就是用这个软件,很喜欢. vue的教程 1.http://www.jianshu.com/p/5ba253651c ...
- Linux内核学习总览
断断续续学习操作系统已经有大半年时间了,一直想系统地梳理一下. 正好借助<深入Linux内核架构> (Wolfgang Manuere 著,郭旭 译)汇总一下. 首先基础框架篇,Linux ...
- 简单的自动化使用--使用selenium实现学习通网站的刷慕课程序。注释空格加代码大概200行不到
简单的自动化使用--使用selenium实现学习通网站的刷慕课程序.注释空格加代码大概200行不到 相见恨晚啊 github地址 环境Python3.6 + pycharm + chrom浏览器 + ...
- tornado框架基础01-路由简介
tornado 小而精 Django 大而全 Web框架 Tornado是一个由Python开发的Web框架 Web服务 利用Tornado,可以快速搭建和一个高性能的Web服务 非阻塞 Tornad ...
- 前端基础之CSS_1
摘要 CSS(层叠样式表)的三种设置方法 基本选择器 组合选择器 属性选择器 分组与嵌套 伪类选择器 伪元素选择器 选择器的优先级 一些样式的设置(字体.文本.背景.边框) display属性设置 0 ...
- 大数据学习——下载集群根目录下的文件到E盘
代码如下: package cn.itcast.hdfs; import java.io.IOException; import org.apache.hadoop.conf.Configuratio ...
- [NOIP1999] 普及组
回文数 /*By SilverN*/ #include<algorithm> #include<iostream> #include<cstring> #inclu ...
- js闭包的用途[转载]
通过使用闭包,我们可以做很多事情.比如模拟面向对象的代码风格:更优雅,更简洁的表达出代码:在某些方面提升代码的执行效率. 1 匿名自执行函数我们知道所有的变量,如果不加上var关键字,则默认的会添加到 ...
- OC温习四:数组
/** arrayByAddingObject -- 往数组A添加一个数据,返回一个数组的形式,即必须有一个数组来接受 */ NSArray *array = [NSArray arrayWithOb ...
- 动态规划:Monkey and Banana
Problem Description A group of researchers are designing an experiment to test the IQ of a monkey. T ...