有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java使用带有自定义注释的hibernateenvers

在我当前的项目中,我们需要以精简的方式(尽可能精简)分发一个包含实体类的JAR,以便与REST客户机一起使用。注释@Entity(来自JPA)不是问题,但是当使用hibernate envers @Audited注释相同的类时,依赖性就成了问题

我目前解决这个问题的想法不是使用实体类的接口,而是创建一个@CustomAuditAnnotattion,并在hibernate引导期间将它们识别为hibernate envers注释

像这样的事情:

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

在冬眠催眠期间:

...
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;
            }
        });
    }
}

我的背景是:JbossAS 7.1.1。最后,Hibernate 4.0.1,上面的方法应该放在一个未分发的JAR中(服务器端)

问题是:

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


共 (0) 个答案