Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/379.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Hibernate中特定于实体的Listner/Event/Interceptor_Java_Spring_Hibernate - Fatal编程技术网

Java Hibernate中特定于实体的Listner/Event/Interceptor

Java Hibernate中特定于实体的Listner/Event/Interceptor,java,spring,hibernate,Java,Spring,Hibernate,我有许多实体(超过1000个),但我需要特定实体的侦听器/侦听器/事件(70-80个)。我不想基于拦截器中的if-else条件进行检查,因为这会使代码变脏 spring/Hibernate中是否有任何注释或接口只侦听该注释。引入标记接口(无方法)并将其添加到所有实体中 在拦截器的方法中,只需检查 public boolean onSave(Object entity,Serializable id, Object[] state,String[] propertyNames,Type[]

我有许多实体(超过1000个),但我需要特定实体的侦听器/侦听器/事件(70-80个)。我不想基于拦截器中的if-else条件进行检查,因为这会使代码变脏


spring/Hibernate中是否有任何注释或接口只侦听该注释。

引入标记接口(无方法)并将其添加到所有实体中

在拦截器的方法中,只需检查

public boolean onSave(Object entity,Serializable id,
    Object[] state,String[] propertyNames,Type[] types)
    throws CallbackException {

    System.out.println("onSave");

    if (entity instanceof IMarkerInterface){
        doSomethingWith(entity);
    }
    return false;
}
或者您可以为实体添加自定义注释,并使用
entity.getClass().getClass().getAnnotation(markeranotation.class)
。只需检查注释是否存在,并相应地应用您的逻辑即可