gpt4 book ai didi

hibernate - 如何使用 spring boot 1.5.x 配置 "hibernate.integrator_provider"

转载 作者:行者123 更新时间:2023-12-03 20:28:31 26 4
gpt4 key购买 nike

我正在使用 springboot 1.5.x 并尝试在 this 之后实现事件监听器教程。

我遇到的阻碍是SpringBoot 1.5.x无法设置hibernate integrator。 我试过在properties.yml中配置integrator像下面的代码,但它抛出一个异常,不能将字符串转换为 Integrator:

spring:
jpa:
properties:
hibernate.integrator_provider: com.xxxxx.RootAwareEventListenerIntegrator

Here是一个相关的问题,但提供的解决方案不适用于 springBoot 1.5.x。

最佳答案

我从 here 找到了一个可用的解决方案.它不使用积分器,而是将所有事件监听器一一添加。下面是我的代码:

public class RootAwareInsertEventListener implements PersistEventListener {

public static final RootAwareInsertEventListener INSTANCE = new RootAwareInsertEventListener();

@Override
public void onPersist(PersistEvent event) throws HibernateException {
final Object entity = event.getObject();

if (entity instanceof RootAware) {
RootAware rootAware = (RootAware) entity;
Object root = rootAware.getRoot();
event.getSession().lock(root, LockMode.OPTIMISTIC_FORCE_INCREMENT);

log.info("Incrementing {} entity version because a {} child entity has been inserted",
root, entity);
}
}

@Override
public void onPersist(PersistEvent event, Map createdAlready)
throws HibernateException {
onPersist(event);
}
}
@Component
public class HibernateListenerConfigurer {

@PersistenceUnit
private EntityManagerFactory emf;

@PostConstruct
protected void init() {
SessionFactoryImpl sessionFactory = emf.unwrap(SessionFactoryImpl.class);
EventListenerRegistry registry = sessionFactory.getServiceRegistry().getService(EventListenerRegistry.class);
registry.getEventListenerGroup(EventType.PERSIST).appendListener(RootAwareInsertEventListener.INSTANCE);
registry.getEventListenerGroup(EventType.FLUSH_ENTITY).appendListener(RootAwareUpdateAndDeleteEventListener.INSTANCE);

}
}

关于hibernate - 如何使用 spring boot 1.5.x 配置 "hibernate.integrator_provider",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55607443/

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