hibernate Sping Boot 2.0.0 M6 -添加休眠拦截器

w80xi6nr  于 2023-01-17  发布在  其他
关注(0)|答案(4)|浏览(138)

从Sping Boot 2.0.0 M2升级到2.0.0 M6后,我的Hibernate拦截器实现不再工作。
我的旧实现:

@Configuration
public class HibernateConfiguration extends HibernateJpaAutoConfiguration {

    private HibernateStatisticsInterceptor hibernateStatisticsInterceptor;

    public HibernateConfiguration(DataSource dataSource, JpaProperties jpaProperties, ObjectProvider<JtaTransactionManager> jtaTransactionManager, ObjectProvider<TransactionManagerCustomizers> transactionManagerCustomizers, ObjectProvider<List<SchemaManagementProvider>> providers, HibernateStatisticsInterceptor hibernateStatisticsInterceptor) {
        super(dataSource, jpaProperties, jtaTransactionManager, transactionManagerCustomizers, providers);
        this.hibernateStatisticsInterceptor = hibernateStatisticsInterceptor;
    }

    @Override
    protected void customizeVendorProperties(Map<String, Object> vendorProperties) {
        vendorProperties.put("hibernate.session_factory.interceptor", hibernateStatisticsInterceptor);
    }
}

但是在M5或M6中,HibernateJpaAutoConfiguration类发生了变化,不再扩展JpaBaseConfiguration。
我尝试在每个YAML配置文件中添加拦截器,但它不起作用。
我的拦截器:

@Component("hibernateStatisticsInterceptor")
public class HibernateStatisticsInterceptor extends EmptyInterceptor {

    private static final long serialVersionUID = 5278832720227796822L;

    private ThreadLocal<Long> queryCount = new ThreadLocal<>();

    public void startCounter() {
        queryCount.set(0l);
    }

    public Long getQueryCount() {
        return queryCount.get();
    }

    public void clearCounter() {
        queryCount.remove();
    }

    @Override
    public String onPrepareStatement(String sql) {
        Long count = queryCount.get();
        if (count != null) {
            queryCount.set(count + 1);
        }
        return super.onPrepareStatement(sql);
    }

}

我怎样才能重新启动我的拦截器?
谢谢你的提示
问候瑞兹

7xzttuei

7xzttuei1#

"你好"
给予一读:https://github.com/spring-projects/spring-boot/commit/59d5ed58428d8cb6c6d9fb723d0e334fe3e7d9be(用途:休眠属性定制器界面)
1.执行它。
1.@覆盖customize()方法,添加拦截器
1.不要忘记**@Lazy inject**以防出现内部bean
1.完成=〉运行
希望这对你有用。
最后一点:始终更新您的Spring / Hibernate版本(尽可能使用最新版本),您将看到大多数代码将变得多余,因为较新的版本试图尽可能减少配置。

wh6knrhe

wh6knrhe2#

我可以通过以下文档解决此问题:
Add support for advanced customization of Hibernate settings
仅实现一个接口 * Hibernate属性定制器 *
并实现方法 * 自定义(Map〈String,Object〉hibernateProperties)*

@Component
class MyHibernateInterceptorCustomizer implements HibernatePropertiesCustomizer {

    @Autowired
    MyInterceptor myInterceptor

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

gkl3eglg3#

由于https://github.com/spring-projects/spring-boot/issues/11211已得到解决,因此自Sping Boot 2.0.0.RC1(在撰写本文时尚未发布,但现在提供快照)起,HibernatePropertiesCustomizer即可使用。

n3schb8v

n3schb8v4#

Sping Boot 中,您可以轻松地注册您的Hibernate自定义拦截器
实现HibernatePropertiesCustomizer接口并覆盖customize方法以将custum inteceptor添加到hibernateProperties

@Component
 public class MyCustomInterceptor extends EmptyInterceptor implements HibernatePropertiesCustomizer {

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

    @Override
    public boolean onSave(Object entity, Serializable id, Object[] state, String[] propertyNames, org.hibernate.type.Type[] types) {
        
        System.out.println("onSave");

        return super.onSave(entity, id, state, propertyNames, types);
    }
}

相关问题