When Envers generates a MOD revision for the far side of a bidirectional collection change (revision_on_collection_change=true, the default), BaseEnversCollectionEventListener passes the raw collection element / owner reference into CollectionChangeWorkUnit without unwrapping Hibernate proxies — unlike the entity-event path, where BaseEnversEventListener#addCollectionChangeWorkUnit unwraps via EntityTools.getTargetFromProxy (HHH-7249).
If the session already holds an uninitialized proxy of the related entity when the collection is processed, the work unit maps the audit row off the proxy wrapper: the property mapper’s reflection field-reads (GetterFieldImpl → raw Field.get) return null for every audited property, so the _AUD MOD row records NULL in every column.
Consequences:
- Silent audit corruption (default config): the entity’s history gains a
MODrevision claiming all its properties becamenull. - Hard failure with
do_not_audit_optimistic_locking_field=falseand a NOT NULL version column in the_AUDschema: the insert binds NULL for the version and rolls back an otherwise-valid entity delete with a constraint violation.
Steps to reproduce
Runnable reproducer: GitHub - apoorvam/envers-collection-change-proxy-repro · GitHub — clone and run mvn test. The single test fails on 7.2.19.Final with expected: <parent> but was: <null>; the README walks through the mechanism in detail.
In short: a Child references the same audited Parent through a lazy @ManyToOne (seeds the uninitialized proxy at em.find(Child)) and an owned bidirectional @ManyToMany; deleting the child fires the collection-remove event whose element resolves to that proxy. Removing either relationship makes the test pass.
Suggested fix
In BaseEnversCollectionEventListener, unwrap the entity reference before constructing CollectionChangeWorkUnit, the same way BaseEnversEventListener#addCollectionChangeWorkUnit does — in both generateBidirectionalCollectionChangeWorkUnits and the owner work units built from event.getAffectedOwnerOrNull() (guarding the null-owner case).