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

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

    If you're using Spring Data JPA then addition @Transactional annotation to your service implementation would solve the issue.

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

    This happens when saving an object when Hibernate thinks it needs to save an object that is associated with the one you are saving.

    I had this problem and did not want to save changes to the referenced object so I wanted the cascade type to be NONE.

    The trick is to ensure that the ID and VERSION in the referenced object is set so that Hibernate does not think that the referenced object is a new object that needs saving. This worked for me.

    Look through all of the relationships in the class you are saving to work out the associated objects (and the associated objects of the associated objects) and ensure that the ID and VERSION is set in all objects of the object tree.

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

    My problem was related to @BeforeEach of JUnit. And even if I saved the related entities (in my case @ManyToOne), I got the same error.

    The problem is somehow related to the sequence that I have in my parent. If I assign the value to that attribute, the problem is solved.

    Ex. If I have the entity Question that can have some categories (one or more) and entity Question has a sequence:

    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "feedbackSeq")
    @Id
    private Long id;
    

    I have to assign the value question.setId(1L);

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

    Just make Constructor of your mapping in your base class. Like if you want One-To-One relation in Entity A, Entity B. if your are taking A as base class, then A must have a Constructor have B as a argument.

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

    I believe this might be just repeat answer, but just to clarify, I got this on a @OneToOne mapping as well as a @OneToMany. In both cases, it was the fact that the Child object I was adding to the Parent wasn't saved in the database yet. So when I added the Child to the Parent, then saved the Parent, Hibernate would toss the "object references an unsaved transient instance - save the transient instance before flushing" message when saving the Parent.

    Adding in the cascade = {CascadeType.ALL} on the Parent's reference to the Child solved the problem in both cases. This saved the Child and the Parent.

    Sorry for any repeat answers, just wanted to further clarify for folks.

    @OneToOne(cascade = {CascadeType.ALL})
    @JoinColumn(name = "performancelog_id")
    public PerformanceLog getPerformanceLog() {
        return performanceLog;
    }
    
    0 讨论(0)
  • 2020-11-22 07:30

    This occurred for me when persisting an entity in which the existing record in the database had a NULL value for the field annotated with @Version (for optimistic locking). Updating the NULL value to 0 in the database corrected this.

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