【问题标题】:How to trigger Hibernate interceptor in the integration test?如何在集成测试中触发Hibernate拦截器?
【发布时间】:2022-12-17 11:09:56
【问题描述】:

我已经为 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();
  }

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

【问题讨论】:

    标签: java hibernate jpa integration-testing interceptor


    【解决方案1】:

    你有没有找到这个问题的答案?升级 spring boot 和 hibernate 版本后,我遇到了同样的问题。我们的onSave()onFlushDirty() 不再被调用,但这只是在集成测试期间。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-01-03
      • 2022-09-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-09-26
      • 2021-05-04
      相关资源
      最近更新 更多