Spring Boot 2.1.2,Hibernate 5:注册Hibernate事件监听器(插入/更新/删除) [英] Spring Boot 2.1.2, Hibernate 5: Register Hibernate Event Listeners (Insert/Update/Delete)

查看:226
本文介绍了Spring Boot 2.1.2,Hibernate 5:注册Hibernate事件监听器(插入/更新/删除)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我在先前的Spring应用程序中使用Hibernate 4时,以下内容足以注册所有对DB Insert/Update/Delete事件的拦截.有一个简单的Configuration类和实际的Interceptor实现.

When I was using Hibernate 4 in a previous Spring application, the following was sufficient to register all interception of DB Insert/Update/Delete events. There was a simple Configuration class and the actual Interceptor implementation.

配置类

@Component
public class HibernateEntityEventListenerConfig {

    @Autowired
    private SessionFactory sessionFactory;

    @Autowired
    private HibernateEntityEventListener entityEventListener;

    @PostConstruct
    public void registerListeners() {
          final EventListenerRegistry registry = ((SessionFactoryImpl) sessionFactory)
                    .getServiceRegistry().getService(EventListenerRegistry.class);
            registry.getEventListenerGroup(EventType.PRE_INSERT).appendListener((PreInsertEventListener) entityEventListener);
            registry.getEventListenerGroup(EventType.PRE_UPDATE).appendListener((PreUpdateEventListener) entityEventListener);
            registry.getEventListenerGroup(EventType.PRE_DELETE).appendListener((PreDeleteEventListener) entityEventListener);
    }

}

拦截器类

@Component
public class HibernateEntityEventListener implements PreInsertEventListener,
                                                     PreUpdateEventListener,    
                                                     PreDeleteEventListener {

@Override
public boolean onPreInsert(PreInsertEvent event) {
  //...
}

@Override
public boolean onPreUpdate(PreUpdateEvent event) {
  //...
}

@Override
public boolean onPreDelete(PreUpdateEvent event) {
  //...
}

但是当我使用Hibernate 5将此代码移植到Spring Boot 2.1.2应用程序时,我开始遇到以下有关未找到 SessionFactory 的启动错误:

But when I ported this code to a Spring Boot 2.1.2 app using Hibernate 5, I started getting the following startup error about SessionFactory not being found:

Field sessionFactory in util.HibernateEntityEventListenerConfig required 
a bean of type 'org.hibernate.SessionFactory' that could not be found.

The injection point has the following annotations:
    - @org.springframework.beans.factory.annotation.Autowired(required=true)


Action:

Consider defining a bean of type 'org.hibernate.SessionFactory' in your configuration.

我在Google周围四处搜寻,但我发现了一些我不信任或无法理解的更复杂的代码, https://stackoverflow.com/a/48471227/1005607

I googled around and I only found some more elaborate and messy code that I don't trust or understand, https://stackoverflow.com/a/48471227/1005607

现在这太复杂了,我只需要一种简单的方法来连接此拦截器.插入拦截器并克服此SessionFactory错误的正确方法是什么?

This is way too complicated now and I just need a simple way to wire this interceptor. What's the right way to put in the interceptors and get past this SessionFactory error?

推荐答案

我在 Bozho的博客,代码可以与以前保持几乎相同,但是我无法再自动连接 SessionFactory :我必须自动连接 EntityManagerFactory 相反,然后将其解包"为 SessinFactoryImpl sf = emf.unwrap(..).那是唯一的变化.

I found the solution on Bozho's blog, the code can stay mostly the same as before, but I can't autwire SessionFactory anymore: I have to autowire EntityManagerFactory instead, and then "unwrap" it as SessinFactoryImpl sf = emf.unwrap(..). That's the only change.

最新代码:配置:

@Component   
public class HibernateEntityEventListenerConfig {



@PersistenceUnit
private EntityManagerFactory emf;  // NOTE Can't autowire SessionFactory.

@Autowired
private HibernateEntityEventListener entityEventListener;

@PostConstruct
public void registerListeners() {
    // NOTE the emf.unwrap() to get the SessionFactoryImpl
    SessionFactoryImpl sessionFactory = emf.unwrap(SessionFactoryImpl.class);
    EventListenerRegistry registry = ((SessionFactoryImpl) sessionFactory)
                .getServiceRegistry().getService(EventListenerRegistry.class);
        registry.getEventListenerGroup(EventType.PRE_INSERT).appendListener((PreInsertEventListener) entityEventListener);
        registry.getEventListenerGroup(EventType.PRE_UPDATE).appendListener((PreUpdateEventListener) entityEventListener);
        registry.getEventListenerGroup(EventType.PRE_DELETE).appendListener((PreDeleteEventListener) entityEventListener);
}

}

这篇关于Spring Boot 2.1.2,Hibernate 5:注册Hibernate事件监听器(插入/更新/删除)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆