Java Hibernate中特定于实体的Listner/Event/Interceptor
我有许多实体(超过1000个),但我需要特定实体的侦听器/侦听器/事件(70-80个)。我不想基于拦截器中的if-else条件进行检查,因为这会使代码变脏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[]
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)
。只需检查注释是否存在,并相应地应用您的逻辑即可