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

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

    Simple way of solving this issue is save the both entity. first save the child entity and then save the parent entity. Because parent entity is depend on child entity for the foreign key value.

    Below simple exam of one to one relationship

    insert into Department (name, numOfemp, Depno) values (?, ?, ?)
    Hibernate: insert into Employee (SSN, dep_Depno, firstName, lastName, middleName, empno) values (?, ?, ?, ?, ?, ?)
    
    Session session=sf.openSession();
            session.beginTransaction();
            session.save(dep);
            session.save(emp);
    

提交回复
热议问题