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

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

    I also faced the same situation. By setting following annotation above the property made it solve the exception prompted.

    The Exception I faced.

    Exception in thread "main" java.lang.IllegalStateException: org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing: com.model.Car_OneToMany
    

    To overcome, the annotation I used.

        @OneToMany(cascade = {CascadeType.ALL})
        @Column(name = "ListOfCarsDrivenByDriver")
        private List listOfCarsBeingDriven = new ArrayList();
    

    What made Hibernate throw the exception:

    This exception is thrown at your console because the child object I attach to the parent object is not present in the database at that moment.

    By providing @OneToMany(cascade = {CascadeType.ALL}) , it tells Hibernate to save them to the database while saving the parent object.

提交回复
热议问题