仅为实体组配置 Hibernate 实体侦听器

Configuring Hibernate Entity Listeners for only group of entities

根据 https://vladmihalcea.com/hibernate-event-listeners/,我想为我的实体组的 post insert/update/delete 事件配置实体侦听器。我不希望我的应用程序中的所有实体都使用这些侦听器。所以我为一组实体创建了一个抽象 class 并实现了 onPostUpdate/onPostInsert 方法。但是我无法为集成器部分配置这个抽象监听器 class,因为它只需要具体的监听器。有什么解决方案推荐吗?

ConfigHibernateEntity

import org.hibernate.event.spi.PostDeleteEvent;
import org.hibernate.event.spi.PostDeleteEventListener;
import org.hibernate.event.spi.PostInsertEvent;
import org.hibernate.event.spi.PostInsertEventListener;
import org.hibernate.event.spi.PostUpdateEvent;
import org.hibernate.event.spi.PostUpdateEventListener;
import org.hibernate.persister.entity.EntityPersister;


public abstract class ConfigHibernateEntity  extends HibernateEntity implements PostUpdateEventListener, PostInsertEventListener, PostDeleteEventListener{


    // public static final ConfigHibernateEntity INSTANCE =  new ConfigHibernateEntity();

    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    @Override
    public void onPostUpdate(PostUpdateEvent event) {

        final Object entity = event.getEntity();

        System.out.println("On post update method");

    }

    @Override
    public void onPostInsert(PostInsertEvent event) {

        final Object entity = event.getEntity();

        System.out.println("On post insert method");

    }

    @Override
    public boolean requiresPostCommitHanding(EntityPersister persister) {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public void onPostDelete(PostDeleteEvent event) {

        final Object entity = event.getEntity();

        System.out.println("On post delete method");

    }
}

事件监听集成器:

import org.hibernate.boot.Metadata;
import org.hibernate.engine.spi.SessionFactoryImplementor;
import org.hibernate.event.service.spi.EventListenerRegistry;
import org.hibernate.event.spi.EventType;
import org.hibernate.integrator.spi.Integrator;
import org.hibernate.service.spi.SessionFactoryServiceRegistry;


public class EventListenerIntegrator implements Integrator {

   @Override
   public void integrate(Metadata metadata, SessionFactoryImplementor 
         sessionFactory, SessionFactoryServiceRegistry serviceRegistry) {

      EventListenerRegistry eventListenerRegistry = 
            serviceRegistry.getService(EventListenerRegistry.class);

      eventListenerRegistry.getEventListenerGroup(EventType.POST_INSERT)
                     .appendListener(new ConfigHibernateEntity());

      eventListenerRegistry.getEventListenerGroup(EventType.POST_UPDATE)
      .appendListener(new ConfigHibernateEntity());


      eventListenerRegistry.getEventListenerGroup(EventType.POST_DELETE)
      .appendListener(new ConfigHibernateEntity());


   }

   @Override
   public void disintegrate(SessionFactoryImplementor sessionFactory,
         SessionFactoryServiceRegistry serviceRegistry) {

   }
}

CustomEntity1 Class:

public class CustomEntity1 extends ConfigHibernateEntity  {

//

}

CustomEntity2 Class :

public class CustomEntity2 extends HibernateEntity  {

//

}

与其在 ConfigHibernateEntity 本身中实现侦听器,不如创建单独的侦听器 class 并检查实体是否是 ConfigHibernateEntity 的实例,

public class ConfigHibernateEntityListener implements PostUpdateEventListener,
PostInsertEventListener, PostDeleteEventListener {

private static final long serialVersionUID = -2607908089881583295L;

private final Predicate<Object> checkInstanceOfConfigHibernateEntity =
    event -> event instanceof ConfigHibernateEntity;

@Override
public void onPostUpdate(final PostUpdateEvent event) {

    final Object entity = event.getEntity();
    if (checkInstanceOfConfigHibernateEntity.test(entity)) {
        System.out.println("On post update method");
    }

}

@Override
public void onPostInsert(final PostInsertEvent event) {

    final Object entity = event.getEntity();
    if (checkInstanceOfConfigHibernateEntity.test(entity)) {
        System.out.println("On post insert method");
    }

}

@Override
public boolean requiresPostCommitHanding(final EntityPersister persister) {
    // TODO Auto-generated method stub
    return false;
}

@Override
public void onPostDelete(final PostDeleteEvent event) {

    final Object entity = event.getEntity();
    if (checkInstanceOfConfigHibernateEntity.test(entity)) {
        System.out.println("On post delete method");
    }

}

ConfigHibernateEntity 可以像下面这样,

/**
 * Marker class for adding configure entity listeners for 
 * post/insert/update/delete
 */
 public class ConfigHibernateEntity {
 }