0

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

Camilo Casadiego
  • 907
  • 3
  • 9
  • 33
  • Why is that the entity name is workflow_step? @Entity(name = "workflow_step") – Harshana Mar 23 '19 at 00:04
  • Just convention, you think it has something to do? Because inserts works just fine, and I also have a prepersist on other entity that works just fine – Camilo Casadiego Mar 23 '19 at 00:11
  • Note that, according to the JPA spec, you should only use entity listeners to modify the *non-relationship state* of an entity. Trying to do anything other than that results in undefined behavior. It may or may not work, and it may also break at any time. You have been warned – crizzis Mar 24 '19 at 10:17
  • @crizzis you are totally right, if you want you can add the answer, all I had to do was to add the code inside the listener to the serviceImpl and it started working :) – Camilo Casadiego Mar 24 '19 at 11:53

1 Answers1

3

Note that, according to the JPA spec, you should only use entity listeners to modify the non-relationship state of an entity.

Trying to do anything other than that results in undefined behavior. It may or may not work, and it may also break at any time. Do not expect authoritative answers on why your specific case does not work either, since trying to add entities in a listener is already sort of a hack and not guaranteed to work.

You have been warned.

crizzis
  • 9,978
  • 2
  • 28
  • 47