How to fix the Hibernate “object references an unsaved transient instance - save the transient instance before flushing” error

前端 未结 30 1058
既然无缘
既然无缘 2020-11-22 07:11

I receive following error when I save the object using Hibernate

object references an unsaved transient instance - save the transient instance before flushi         


        
30条回答
  •  隐瞒了意图╮
    2020-11-22 07:34

    This isn't the only reason for the error. I encountered it just now for a typo error in my coding, which I believe, set a value of an entity which was already saved.

    X x2 = new X();
    x.setXid(memberid); // Error happened here - x was a previous global entity I created earlier
    Y.setX(x2);
    

    I spotted the error by finding exactly which variable caused the error (in this case String xid). I used a catch around the whole block of code that saved the entity and printed the traces.

    {
       code block that performed the operation
    } catch (Exception e) {
       e.printStackTrace(); // put a break-point here and inspect the 'e'
       return ERROR;
    }
    

提交回复
热议问题