如何删除detached instance?

      昨天在做EJB底层DAO的开发时,抱着试试心态传了一个Object进去(之前一直是传Id,然后在删除之前find对象),谁知用Junit测试时竟报出这个异常:

 

      java.lang.IllegalArgumentException: Removing a detached instance

      意思就是说在删除一个detached instance(已分离)对象时出错

 

      之前出错代码:

 

      @PersistenceContext

       private EntityManager entityManager;
       
       
       public void delete(SASVo  vo) {
               entityManager.remove(vo);
        }

       这段代码看上去似乎没有任何问题,但执行就出错,之后查阅相关资料关于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绑定到当前的Sesssion,在用当前Sesssion删除此instance。修改后的代码如

      下:

       @PersistenceContext

       private EntityManager entityManager;
       
       
       public void delete(SASVo  vo) {
               entityManager.remove( entityManager.merge(vo));
        }

 

 

GC Root: Global variable in native code ─ dalvik.system.PathClassLoader instance Leaking: NO (NetSpeedManager$b↓ is not leaking and A ClassLoader is never leaking) ↓ ClassLoader.runtimeInternalObjects ─ java.lang.Object[] array Leaking: NO (NetSpeedManager$b↓ is not leaking) ↓ Object[1626] ─ com.konka.apkhall.edu.module.album.player.presenter.NetSpeedManager$b class Leaking: NO (a class is never leaking) ↓ static NetSpeedManager$b.a ~ ─ com.konka.apkhall.edu.module.album.player.presenter.NetSpeedManager instance Leaking: UNKNOWN Retaining 6.7 MB in 75213 objects ↓ NetSpeedManager.listeners ~~~~~~~~~ ─ java.util.Collections$SynchronizedRandomAccessList instance Leaking: UNKNOWN Retaining 6.7 MB in 75212 objects ↓ Collections$SynchronizedCollection.c ~ ─ java.util.ArrayList instance Leaking: UNKNOWN Retaining 6.7 MB in 75211 objects ↓ ArrayList[8] ~~~ ─ com.konka.apkhall.edu.module.album.player.widgets.VideoStatusView instance Leaking: YES (View.mContext references a destroyed activity) Retaining 1.3 MB in 15235 objects View not part of a window view hierarchy View.mAttachInfo is null (view detached) View.mID = R.id.video_status View.mWindowAttachCount = 1 mContext instance of com.konka.apkhall.edu.module.album.AlbumActivity with mDestroyed = true ↓ View.mContext → com.konka.apkhall.edu.module.album.AlbumActivity instance Leaking: YES (ObjectWatcher was watching this because com.konka.apkhall.edu.module.album.AlbumActivity received ​ Activity#onDestroy() callback and Activity#mDestroyed is true) ​ Retaining 1.0 MB in 11484 objects ​ key = 9ca61e77-8cfc-471d-94d2-e64c3b173343 ​ watchDurationMillis = 32423 retainedDurationMillis = 27421 mApplication instance of com.konka.apkhall.edu.app.EduApplication ​ mBase instance of android.app.ContextImpl分析原因,给出具体解决代码
最新发布
05-24
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值