Hello,
In the project I’m working on, I’m using Hibernate Envers to track changes to entities. I’ve encountered an issue where I want to audit only the base class AbstractDict
(shown below), but Envers doesn’t seem to detect any changes unless I also annotate each subclass with @Audited
.
Here’s the simplified structure:
@Audited
@AuditTable(schema = "audit", value = "abstract_dict_aud")
@Entity
@Table(name = "abstract_dict", schema = "dictionaries")
@AllArgsConstructor
@NoArgsConstructor
@Getter
@Setter
@Inheritance(strategy = InheritanceType.JOINED)
@JsonSubTypes({
@JsonSubTypes.Type(value = GlossaryOfMeasureUnits.class, name = "MIERNIKI"),
@JsonSubTypes.Type(value = GlossaryOfProcesses.class, name = "PROCESY"),
@JsonSubTypes.Type(value = Area.class, name = "OBSZAR"),
@JsonSubTypes.Type(value = ClientParameters.class, name = "PARAMETRY_KLIENTA"),
@JsonSubTypes.Type(value = DocumentTypes.class, name = "TYPY_DOKUMENTOW"),
// ... potentially 50+ more types
})
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, property = "dictionaryName", visible = true)
public abstract class AbstractDict extends AbstractAuditingEntity implements Serializable {
// ...
}
Currently, Envers does not audit changes to AbstractDict
unless I also annotate each subclass like this:
@Audited
@AuditTable(schema = "audit", value = "document_types_aud")
@Entity
@Table(name = "document_types", schema = "dictionaries")
@Getter
@Setter
public class DocumentTypes extends AbstractDict implements Serializable {
@Override
protected AbstractDict createNewWithExtendedFields() {
return new DocumentTypes();
}
}
This, however, leads to 50+ _AUD
tables and additional schema clutter, which I’d prefer to avoid.
Moreover, to maintain referential integrity, I have to manually define foreign key constraints between subclass _aud
tables and the base audit table:
ADD CONSTRAINT fkc2adk6hk7ew5uxxxx2qu16pe2
FOREIGN KEY (id, rev)
REFERENCES audit.abstract_dict_aud(id, rev)
DEFERRABLE INITIALLY DEFERRED;
My questions:
- Is there a way to configure Envers to only audit the base class (
AbstractDict
) and ignore its subclasses, while still tracking the changes made through them? - Alternatively, is there a way to map all subclass changes to a single audit table (
abstract_dict_aud
) instead of generating one_AUD
table per subclass?
I’ve read the official Envers documentation but couldn’t find an annotation or config that solves this.
Any insights or workarounds would be much appreciated!