IllegalStateException:存储实体< entity>时发生错误实体拷贝<实体>已被分配给不同的实体< entity_copy> [英] IllegalStateException: Error occurred while storing entity <entity> An entity copy <entity> was already assigned to a different entity <entity_copy>

查看:359
本文介绍了IllegalStateException:存储实体< entity>时发生错误实体拷贝<实体>已被分配给不同的实体< entity_copy>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

经过大量的谷歌搜索,我没有找到我的问题的答案,除了降级休眠版本。但我在2003年的类似职位上遇到过这种情况。



问题:

  //在第一个会话中,我做
session1.save(entity);
session1.getTransaction()。commit();
session1.close();

//在第二个会话中(客户端响应之后),我找回了实体的序列化副本
entityCopy = deserialize(jsonString);
entityCopy.setEntityDetail(newDetail); // 1
session2.merge(entityCopy); //例外!

如果注释字符串1,一切正常!



异常:
$ b IllegalStateException 实体#4700实体副本#4700已被分配给不同的实体@ 2e7b134a



问题:


  1. 我的感觉出了什么问题?

  2. 据我所知,merge()被实现了这些情况下,当我们可以有实体拷贝缓存。


    1. 如果重要实体 - > EntityDetail lazy,orphanRemoval = true链接,则一对一 关系

    2. 我重写了equals()和hashCode()方法。


    解决方案

    我以下面的方式解决了这个问题:反序列化的实体,然后再对其进行一些更改。 (唯一的变化是2字符串):

      //在第一个会话中,我做
    session1.save (实体);
    session1.getTransaction()。commit();
    session1.close();

    //在第二个会话中(客户端响应之后),我找回了实体的序列化副本
    entityCopy = deserialize(jsonString);
    entityCopy =(Entity)session.merge(entityCopy); // 2
    entityCopy.setEntityDetail(newDetail);
    session2.merge(entityCopy); // all works fine


    After a lot of googling I didn't find answer to my question, except downgrade hibernate version. But I faced this situation at similar post dated as 2003 year.

    What the problem:

    //in the  first session I do
    session1.save(entity);
    session1.getTransaction().commit();
    session1.close();
    
    //in the second session (after client response), I get back the serialized copy of the entity
    entityCopy = deserialize(jsonString);
    entityCopy.setEntityDetail(newDetail); //1
    session2.merge(entityCopy); //EXCEPTION!
    

    If comment string 1, all works fine!

    Exception:

    IllegalStateException: Error occurred while storing entity #4700 An entity copy #4700 was already assigned to a different entity @2e7b134a

    Questions:

    1. What is wrong in my sitation?
    2. As I understand, merge() operation was implemented for those cases, when we alreadty have the entity copy in cache. Am I wrong?

    PS

    1. If it is important Entity -> EntityDetail are linked with lazy, orphanRemoval = true, one-2-one relationship
    2. I overrided equals() and hashCode() methods.

    解决方案

    I solve this problem in the next way: it is necessary to merge deserialized entity before you proceed some changes to it. (the only change is in the 2 string):

    //in the  first session I do
    session1.save(entity);
    session1.getTransaction().commit();
    session1.close();
    
    //in the second session (after client response), I get back the serialized copy of the entity
    entityCopy = deserialize(jsonString);
    entityCopy = (Entity) session.merge(entityCopy); //2
    entityCopy.setEntityDetail(newDetail); 
    session2.merge(entityCopy); //all works fine
    

    这篇关于IllegalStateException:存储实体< entity>时发生错误实体拷贝<实体>已被分配给不同的实体< entity_copy>的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆