本文介绍了使用 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 比拦截器更好,并且代码和配置都在一个地方.缺点是配置更长,涉及更多.

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 拦截器或侦听器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 21:26