Envers behviour inconsistent when @Version field auditing is enabled

I have @Version field auditing enabled:

org.hibernate.envers.do_not_audit_optimistic_locking_field=false

I have a parent object (Parent) which has both @OneToOne (Address) and @OneToMany (Child) child properties.
The @OneToOne Address property is mapped using @JoinColumn.
The @OneToMany (Child/children) property is actually a List mapped using a @JoinColumn & @OrderColumn
The parent object has a @Version field - the child entities do not.

If I modify any of the child entities and call .merge(parent), the parent’s @Version field is incremented & a new audit entry is inserted for the parent object. This behaviour is exactly what I want.

However, if the @OneToMany property collection is empty then the parent’s @Version is NOT incremented and no new audit entry for parent.

Quite literally, whether the (completely unrelated) collection is empty or not changes the behaviour of @Version & Auditing. That seems wrong.

It is not clear which is the “correct” behaviour - does anyone know a way I can “force” the @Version update & audit ?

Here are my objects:

Parent (has version field - for optimistic concurrency control)

@Audited
@Entity
public class Parent {
    @Id
    @Column
    private UUID id;

    @Column
    private String name;

    @OneToOne(cascade = CascadeType.ALL, orphanRemoval = true, fetch = FetchType.EAGER)
    @JoinColumn(name = "address_id", nullable = false)
    private Address address;

    @OneToMany(cascade = CascadeType.ALL, orphanRemoval = true, fetch = FetchType.EAGER)
    @JoinColumn(name = "parent_id", nullable = false, updatable = false)
    @OrderColumn(name = "child_index", nullable = false)
    private List<Child> children = new ArrayList<>();

    @Version
    @Column(name = "VERSION", nullable = false)
    private Integer version;
}

Address (this is what we are updating)

@Audited
@Entity
public class Address {
    @Id
    @Column
    private UUID id;

    @Column
    private String name;
}

Child (not updated - but mere presence seems to magically affect behaviour)

@Audited
@Entity
public class Child {
    @Id
    @Column
    private UUID id;

    @Column
    private String name;
}

Can you please create a test case that reproduces this behavior? Sounds like a bug to me, but we will have to see evidence of that.

Thanks @beikov - already raised - [HHH-15249] - Hibernate JIRA

What is the expected behaviour? (the @Version should increment or no ?)

Digging into the code, it appears that the determination of dirty collection is wrong.
It will be entertaining if it ends up being related to this bug I raised some time ago - [HHH-13400] - Hibernate JIRA :slight_smile:

Having a rushed look at the code, it looks like it will always detect a dirty collection & increment the Version if the collection is non-empty. This doesn’t feel right. It might also explain [HHH-13400].

Is there a way to get the parent @Version to always increment if a child object is updated ?

Thanks for creating the bug report. I didn’t dig into this yet and am not sure when I will be able to do so, but I think you are right to believe that this is a bug.