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;
}