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?