带有 Spring Boot 和 Spring Data JPA 的 Hibernate 拦截器或侦听器 [英] Hibernate interceptor or listener with Spring Boot and Spring Data JPA

查看:25
本文介绍了带有 Spring Boot 和 Spring Data JPA 的 Hibernate 拦截器或侦听器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在保存对象的子级集合之前运行一些检查(级联 = 全部).

I'd like to run some checks prior to saving a collection of children of an object (cascade = all).

我正在使用 Spring Boot 和 Spring Data JPA,想知道哪种方法最好:Hibernate 侦听器或拦截器.每个的优点/缺点是什么?您是否碰巧有您认为最佳方法的示例?

I am using Spring Boot and Spring Data JPA and was wondering what approach would be the best: a Hibernate listener or an interceptor. What are the pros/cons of each ? Do you happen to have an example for the one you consider the best approach ?

在像这样在 XML 中配置之前,我使用过 Hibernate 侦听器:

I have used Hibernate listeners before configured in XML like this:

    <property name="eventListeners">
        <map>
            <entry key="post-update">
                <list>
                    <ref bean="myListener" />
                </list>
            </entry>
        </map>
    </property>

在会话工厂(旧项目)上.但是现在我的大部分配置都在注释中(导致 Spring Boot),我希望使配置尽可能简单明了,因此拦截器可能是更好的解决方案.

on the session factory (older project). But now most of my configs are in annotations (cause Spring Boot) and I want to keep the configs as simple and light as possible, so maybe an interceptor would be a better solution.

谢谢.

推荐答案

我为自己做了很多环顾四周,并认为我会分享我的工作(我包括了有用的(非内联)链接在底部).

I did a lot of looking around on this for myself and thought I'd share what I got working (I included the helpful (non-inline) links at the bottom).

要使用拦截器,您需要扩展 org.hibernate.EmptyInterceptor 类并覆盖要拦截的方法.你可能想要 onSave(...) 在你的情况下.

To use an interceptor, you extend the org.hibernate.EmptyInterceptor class and override the methods you want to intercept. You probably want onSave(...) in your case.

package foo.bar;

import org.hibernate.EmptyInterceptor;
import org.hibernate.type.Type;
import java.io.Serializable;

public class MyInterceptor extends EmptyInterceptor {
    @Override
    public boolean onSave(Object entity, Serializable id, Object[] state, String[] propertyNames, Type[] types) {
        // do your checks here
        return false;
    }
}

您必须注册您的拦截器 与 Spring/Hibernate.您可以在 application.properties 或 application.yml.

You have to register your interceptor with Spring/Hibernate. You can do this in your application.properties or application.yml.

spring:
  jpa:
    properties:
      hibernate.ejb.interceptor: foo.bar.MyInterceptor

拦截器的优点是它(可能)更少的代码和相对简单的配置.缺点是您的整个应用程序只能有一个,而且 API 使用起来可能会令人困惑.

The upsides to an interceptor are that it is (potentially) less code and relatively simple configuration. The downsides are that you can only have one for your entire application and the API can be confusing to work with.

对于事件,您实现 Hibernate 的 org.hibernate.event.spi.*Listener 接口之一.您可能需要 org.hibernate.event.spi.PreInsertEventListener 在你的情况下.

For events, you implement one of Hibernate's org.hibernate.event.spi.*Listener interfaces. You probably want the org.hibernate.event.spi.PreInsertEventListener in your case.

您必须在 EventListenerRegistry 中注册您的事件.为此,您可以将您的类设为 @Component@AutowireEntityManagerFactory 到您的类中,并创建一个 @PostConstruct 方法来注册你的类.

You have to register your event in the EventListenerRegistry. To do this, you can make your class a @Component, @Autowire the EntityManagerFactory into your class, and create a @PostConstruct method to register your class.

package foo.bar;

import org.hibernate.event.service.spi.EventListenerRegistry;
import org.hibernate.event.spi.EventType;
import org.hibernate.event.spi.PreInsertEvent;
import org.hibernate.event.spi.PreInsertEventListener;
import org.hibernate.internal.SessionFactoryImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import javax.persistence.EntityManagerFactory;

@Component
public class MyEventListener implements PreInsertEventListener {
    @Autowired
    private EntityManagerFactory entityManagerFactory;

    @PostConstruct
    private void init() {
        SessionFactoryImpl sessionFactory = entityManagerFactory.unwrap(SessionFactoryImpl.class);
        EventListenerRegistry registry = sessionFactory.getServiceRegistry().getService(EventListenerRegistry.class);
        registry.getEventListenerGroup(EventType.PRE_INSERT).appendListener(this);
    }

    @Override
    public boolean onPreInsert(PreInsertEvent preInsertEvent) {
        // do your checks here
        return false;
    }
}

侦听器的优点是你可以拥有任意数量的 API,API 比拦截器更好,代码和配置都集中在一个地方.缺点是配置更长、更复杂.

The upsides to listeners are that you can have as many as you want, the API is nicer than the interceptor's, and the code and the configuration are all in one place. The downside is that the configuration is longer and more involved.

这篇关于带有 Spring Boot 和 Spring Data JPA 的 Hibernate 拦截器或侦听器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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