@MappedSuperClass with @Audited working yet causing an error

37 Views Asked by At

I've found that there are a number of entity classes that use the same primary key id type.

For example,

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    //...

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

Adhering to the principle of DRY (Do not repeat yourself) I thought it would make sense to turn this into a generic abstract parent class for all subclasses to use (and to some extent it does).

@MappedSuperclass
public abstract class LongIdEntity {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    public LongIdEntity() {}

    public LongIdEntity(Long id) { 
        this.id = id;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }
}

My issue arrises when I attempt to use this abstract parent class with a subclass that is also annotated with @org.hibernate.envers.Audited.

For some reason, Hibernate throws the following error when any subclass is mapped with @org.hibernate.envers.Audited and extends LongIdEntity.

ERROR 63328 --- [persistence] [           main] o.h.metamodel.internal.MetadataContext   : HHH015007: Illegal argument on static metamodel field injection : org.hibernate.envers.DefaultRevisionEntity_#class_; expected type :  org.hibernate.metamodel.model.domain.internal.EntityTypeImpl; encountered type : jakarta.persistence.metamodel.MappedSuperclassType

Now the I've tried a number of things but before delving into any attempts I'd like to note that even with the error, Hibernate still constructs the tables as originally intended (that is the subclass gets its own unique id column that is audited and the resulting syntax of the code is significantly reduced since we only need to declare the Long id once in the entire application and can reuse it as necessary).

As for the attempts I tried,

  1. Annotating both the parent and subclass with @Audited - which threw the same error

  2. Annotating the subclass only with @Audited - which is the setting I am currently using resulting in functional hibernate despite the error message.

  3. Annotating just the super class which doesn't do anything (this is intentional) as the parent classes fields get inherited into the subclasses as if they were their own columns, resulting in the parent class not having their own table and by extension not being audited. In addition the error goes away which is sort of bizarre considering the error states that hibernate expected an entity type and got a mappedsuperclass type and both of these annotations are being applied to the same parent class simultaneously.

I'm fine leaving it as is but I worry about these kinds of things because even though it is technically working now, this may result in some unintended consequences/bugs in the future.

0

There are 0 best solutions below