Two bidirectional association at the same entity – Column cannot be null


I have a scenario where I have a relationship between two tables. One being the step and the other being to know who is the next step.

enter image description here

select * from cpo_workflow_step_control;

enter image description here

Step entity:

@Table(name = "cpo_workflow_step")
public class CpoWorkflowStep implements {

    @Column(name = "workflow_step_id")

    @OneToMany(fetch = FetchType.EAGER, mappedBy = "cpoWorkflowStepByWorkflowNextStepId", 
            cascade = CascadeType.ALL, orphanRemoval = true)
    private Set<CpoWorkflowStepControl> cpoWorkflowStepControlsForWorkflowNextStepId 
            = new HashSet<CpoWorkflowStepControl>(0);

    @OneToMany(fetch = FetchType.EAGER, mappedBy = "cpoWorkflowStepByWorkflowStepId", 
            cascade = CascadeType.ALL, orphanRemoval = true)
    private Set<CpoWorkflowStepControl> cpoWorkflowStepControlsForWorkflowStepId 
            = new HashSet<CpoWorkflowStepControl>(0);


@Table(name = "cpo_workflow_step_control")
public class CpoWorkflowStepControl implements {

    @Column(name = "workflow_step_control_id")
    private String workflowStepControlId;

    @ManyToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
    @JoinColumn(name = "workflow_next_step_id", nullable = false)
    private CpoWorkflowStep cpoWorkflowStepByWorkflowNextStepId;

    @ManyToOne(fetch = FetchType.EAGER)
    @JoinColumn(name = "workflow_step_id", nullable = false)
    private CpoWorkflowStep cpoWorkflowStepByWorkflowStepId;

My problem is, when I try to save a Step, with a Step_Control associated, JPA tries to save everything and it complains that the last object is missing some properties. Because the last Step, in the propertie cpoWorkflowStepControlsForWorkflowStepId was not save yet.

16:05:07.587 [http-nio-8004-exec-1] WARN  o.h.e.jdbc.spi.SqlExceptionHelper - SQL Error: 1048, SQLState: 23000
16:05:07.587 [http-nio-8004-exec-1] ERROR o.h.e.jdbc.spi.SqlExceptionHelper - Column 'workflow_id' cannot be null

So there is a way to tell JPA what is the order to save all Steps first before to save Step_Control? Do I need to save all Steps before to save Step_Control separated?

I’m using save method from JpaRepository to save a Workflow object with all Steps and its relations inside. 1 – Workflow -> *Step -> *Step_Control;


I think in this case is better first to save all steps, before any step control.

I would at least delete the cascade = CascadeType.ALL from the @OneToMany annotation. After this I would first save the steps and the with the steps saved, the step control.

The problem of the mapping you have is that step is trying to save step control, but also step control is trying to save step, the saves shall be just in one direction in order to avoid these problems.

Also I wanted to mention the you should use the fetch = FetchType.EAGER property very carefully as it can lead you to a memory leak problem.

Answered By – William Andrés Bernal

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published