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

前端 未结 30 1719
既然无缘
既然无缘 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

    beside all other good answers, this could happen if you use merge to persist an object and accidentally forget to use merged reference of the object in the parent class. consider the following example

    merge(A);
    B.setA(A);
    persist(B);
    

    In this case, you merge A but forget to use merged object of A. to solve the problem you must rewrite the code like this.

    A=merge(A);//difference is here
    B.setA(A);
    persist(B);
    

提交回复
热议问题