集成测试中如何触发Hibernate拦截器?

发布于 2025-01-13 09:38:33 字数 995 浏览 3 评论 0原文

我已经为 boolean onFlushDirty 方法创建了拦截器。

注册它就像

@Component
public class HibernateInterceptorRegistration implements HibernatePropertiesCustomizer {

  // My interceptor
  private final EntityInterceptor entityInterceptor;

  public HibernateInterceptorRegistration(EntityInterceptor entityInterceptor) {
    this.entityInterceptor = entityInterceptor;
  }

  @Override
  public void customize(Map<String, Object> hibernateProperties) {
    hibernateProperties.put("hibernate.session_factory.interceptor", entityInterceptor);
  }
}

应用程序工作正常一样,但我无法在集成测试中触发此拦截器。 我有 TestConfig 类,我 @Import 到集成测试类。

我在那里添加了 2 个豆子:

  @Bean
  public HibernatePropertiesCustomizer hibernatePropertiesCustomizer() {
    return new HibernateInterceptorRegistration(entityInterceptor());
  }

  @Bean
  public EntityInterceptor entityInterceptor() {
    return new EntityInterceptor();
  }

没有任何帮助。在测试中使用拦截器应该怎么做?

I have created interceptor for boolean onFlushDirty method.

Registered it like that

@Component
public class HibernateInterceptorRegistration implements HibernatePropertiesCustomizer {

  // My interceptor
  private final EntityInterceptor entityInterceptor;

  public HibernateInterceptorRegistration(EntityInterceptor entityInterceptor) {
    this.entityInterceptor = entityInterceptor;
  }

  @Override
  public void customize(Map<String, Object> hibernateProperties) {
    hibernateProperties.put("hibernate.session_factory.interceptor", entityInterceptor);
  }
}

Application works correct, but I cannot trigger this interceptor in integrational test.
I have TestConfig class, that I @Import to the integrational test class.

There I've added 2 beans:

  @Bean
  public HibernatePropertiesCustomizer hibernatePropertiesCustomizer() {
    return new HibernateInterceptorRegistration(entityInterceptor());
  }

  @Bean
  public EntityInterceptor entityInterceptor() {
    return new EntityInterceptor();
  }

Nothing helped. What should I do to use interceptor in tests?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文