1

Im my current project we have a need to distribute a JAR with entity classes, to be used with a REST client, in a thin way (thinner as possible). The annotations @Entity (from JPA) are not a problem, but when the same classes are annotated with hibernate-envers @Audited the depencies become a problem.

Instead of work with Interfaces for entity classes, my current idea to solve this problem includes a creation of a @CustomAuditAnnotattion and, during the hibernate bootstraping, to recognize them as hibernate-envers annotations.

Some thing like this:

@Entity
@CustomAuditedAnnotation
public class MyEnity implements Serializable {
...
}

And, during the hibernate boosstrapping:

...
public void onEntityClassLoad(Class<? extends Serializable> clazz) {
    final CustomAuditAnnotation auditAnn = clazz.getAnnotation(CustomAuditAnnotation.class);

    if (auditAnn != null) {
        Field annField = clazz.getDeclaredField("annotations");
        annField.setAccessible(true);
        @SuppressWarnings("unchecked")
        Map<Class<? extends Annotation>, Annotation> annotations = (Map<Class<? extends Annotation>, Annotation>) annField
                .get(clazz);
        annotations.put(Audited.class, new Audited() {

            @Override
            public Class<? extends Annotation> annotationType() {
                return Audited.class;
            }

            @Override
            public RelationTargetAuditMode targetAuditMode() {
                return RelationTargetAuditMode.valueOf(auditAnn.targetAuditMode());
            }

            @Override
            public ModificationStore modStore() {
                return null;
            }

            @Override
            public Class[] auditParents() {
                return null;
            }
        });
    }
}

My context is: JbossAS 7.1.1.Final, Hibernate 4.0.1, the method above should stay inside a JAR not distributed (server side).

The question:

How to intercept the moment that hibernate loads the mapped entity classes?

  • 1
    How do you intend to control scenarios where certain attributes in a class aren't to be audited while others are? How do you intend to handle other Envers annotations such as `AuditMappedBy`, `AuditJoinTable`, etc. I understand the concern with Envers annotations requiring the full Hibernate stack, but the same would be true if you used any custom Hibernate annotations too, right? – Naros Oct 13 '16 at 16:06
  • This could be an issue, indeed, but currently there's no need of such annotations. Until the hibernate-envers XML configuration implementation, there's the only way... create "thin annotations" as substitutes, I think. – pedrociarlini Oct 17 '16 at 16:01

0 Answers0