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

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

    In my case using Cascade.ALL was adding unnecessary entry to the table, which already had same object (Generating a child value without an id does just that). So I would have to work around with getting child object from repository first, and setting it into the main object instead of the one causing the issue.

    Student s = studentRepo.findByName(generatedObject.getStudent().getName())
    generatedObject.student(s);   
    

提交回复
热议问题