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

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

    If your collection is nullable just try: object.SetYouColection(null);

    0 讨论(0)
  • 2020-11-22 07:21

    I think is because you have try to persist an object that have a reference to another object that is not persist yet, and so it try in the "DB side" to put a reference to a row that not exists

    0 讨论(0)
  • 2020-11-22 07:22

    One possible cause of the error is the inexistence of the setting of the value of the parent entity ; for example for a department-employees relationship you have to write this in order to fix the error :

    Department dept = (Department)session.load(Department.class, dept_code); // dept_code is from the jsp form which you get in the controller with @RequestParam String department
    employee.setDepartment(dept);
    
    0 讨论(0)
  • 2020-11-22 07:24

    This issue happened to me when I created a new entity and an associated entity in a method marked as @Transactional, then performed a query before saving. Ex

    @Transactional
    public someService() {
        Entity someEntity = new Entity();
        AssocaiatedEntity associatedEntity = new AssocaitedEntity();
        someEntity.setAssociatedEntity(associatedEntity);
        associatedEntity.setEntity(someEntity);
    
        // Performing any query was causing hibernate to attempt to persist the new entity. It would then throw an exception
        someDao.getSomething();
    
        entityDao.create(someEntity);
    }
    

    To fix, I performed the query before creating the new entity.

    0 讨论(0)
  • 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<Car_OneToMany> listOfCarsBeingDriven = new ArrayList<Car_OneToMany>();
    

    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.

    0 讨论(0)
  • 2020-11-22 07:25

    I was facing the same error for all PUT HTTP transactions, after introducing optimistic locking (@Version)

    At the time of updating an entity it is mandatory to send id and version of that entity. If any of the entity fields are related to other entities then for that field also we should provide id and version values, without that the JPA try to persist that related entity first as a new entity

    Example: we have two entities --> Vehicle(id,Car,version) ; Car(id, version, brand); to update/persist Vehicle entity make sure the Car field in vehicle entity has id and version fields provided

    0 讨论(0)
提交回复
热议问题