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

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

    In my case it was caused by not having CascadeType on the @ManyToOne side of the bidirectional relationship. To be more precise, I had CascadeType.ALL on @OneToMany side and did not have it on @ManyToOne. Adding CascadeType.ALL to @ManyToOne resolved the issue. One-to-many side:

    @OneToMany(cascade = CascadeType.ALL, mappedBy="globalConfig", orphanRemoval = true)
    private SetgcScopeSet;
    

    Many-to-one side (caused the problem)

    @ManyToOne
    @JoinColumn(name="global_config_id")
    private GlobalConfig globalConfig;
    

    Many-to-one (fixed by adding CascadeType.PERSIST)

    @ManyToOne(cascade = CascadeType.PERSIST)
    @JoinColumn(name="global_config_id")
    private GlobalConfig globalConfig;
    

提交回复
热议问题