如何配置";hibernate.integrator“提供程序”;使用springboot 1.5.x
我正在使用springboot 1.5.x,并尝试在下面的教程中实现一个事件侦听器 我遇到的拦截器是,我无法使用SpringBoot 1.5.x设置hibernate integrator。我曾尝试在如何配置";hibernate.integrator“提供程序”;使用springboot 1.5.x,hibernate,spring-boot,jpa,Hibernate,Spring Boot,Jpa,我正在使用springboot 1.5.x,并尝试在下面的教程中实现一个事件侦听器 我遇到的拦截器是,我无法使用SpringBoot 1.5.x设置hibernate integrator。我曾尝试在properties.yml中配置integrator,如下面的代码所示,但它引发了一个无法将字符串强制转换为integrator的异常: spring: jpa: properties: hibernate.integrator_provider: com.xxxxx.Ro
properties.yml
中配置integrator,如下面的代码所示,但它引发了一个无法将字符串强制转换为integrator的异常:
spring:
jpa:
properties:
hibernate.integrator_provider: com.xxxxx.RootAwareEventListenerIntegrator
是一个相关的问题,但提供的解决方案不适用于springBoot 1.5.x。我从中找到了一个可用的解决方案。它不使用integrator,而是逐个添加所有事件侦听器。下面是我的代码:
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);
}
}