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

前端 未结 30 1100
既然无缘
既然无缘 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: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);

提交回复
热议问题