gpt4 book ai didi

java - 仅为实体组配置 Hibernate 实体监听器

转载 作者:行者123 更新时间:2023-12-01 16:47:40 25 4
gpt4 key购买 nike

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

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");

}
}

EventListenerIntegrator:

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 类:

public class CustomEntity1 extends ConfigHibernateEntity  {

//

}

CustomEntity2 类:

public class CustomEntity2 extends HibernateEntity  {

//

}

最佳答案

不要在 ConfigHibernateEntity 本身中实现监听器,而是创建单独的监听器类并检查该实体是否是 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 {
}

关于java - 仅为实体组配置 Hibernate 实体监听器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61744924/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com