I have a Spring project that uses lombok and a JPA annotated class defined this way:
@EqualsAndHashCode
@AllArgsConstructor
@NoArgsConstructor
@Data
@Entity(name = "workflow_step")
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name = "workflow_step_type")
@SuperBuilder
public abstract class BaseWorkflowStep extends BaseEntity {
@OneToMany(mappedBy = "step", cascade = CascadeType.PERSIST)
protected List<WorkflowEvent> events = Collections.synchronizedList(new ArrayList());
@PreUpdate
public void onPreUpdate() {
if (completed == true) {
System.out.println("Event added!!!\n\n\n\n\n\n");
events.add(WorkflowEvent.builder().workflow(workflow).step(this).eventType(WorkflowEventType.STEP_COMPLETED).build());
}
if (workflow.isCompleted()) {
System.out.println("Event added!!!\n\n\n\n\n\n");
events.add(WorkflowEvent.builder().workflow(workflow).step(this).eventType(WorkflowEventType.COMPLETED)
.build());
}
}
On my tests, the PreUpdate method is always called, but for some reason, when I debug it, it actually generates the inserts into the event table like this:
Hibernate:
/* insert eu.hnpgroup.icare.model.WorkflowEvent
*/ insert
into
workflow_event
(created, by_id, event_type, step_id, text, workflow_id)
values
(?, ?, ?, ?, ?, ?)
But when I just run it, it doesn't and the table is kept clean.
In both cases, the output is printed, but only when debugging it actually works.
Already have checked PreUpdate not firing when adding to a collection and similar posts, but in my case, the method is actually called, only that it doesn't store anything.
One thing worth mentioning is that I have another entity with a similar collection only that it stores the value on a prePersist and it works as expected