Hibernate 和 Spring 在提交到 DB 之前修改查询 [英] Hibernate and Spring modify query Before Submitting to DB

查看:22
本文介绍了Hibernate 和 Spring 在提交到 DB 之前修改查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个配置为使用 Spring Data(@Repository)注释执行的 Hibernate 查询.我想在查询提交到数据库之前捕获查询,当满足特定条件时,稍微修改一下.

I have a Hibernate query that is configured to execute with Spring Data (the @Repository) annotation. I want to catch the query before it is submitted to the DB and when a specific condition is met, modify it a little bit.

这样做的最佳选择是什么?我更喜欢 JPA 解决方案而不是 Hibernate 解决方案,但 Hibernate 解决方案也适用.

What is the best option to do so? I prefere a JPA solution rather then a Hibernate solution, but a Hibernate solution will also work.

推荐答案

如果你想在prepared statement之后修改查询,可以在Hibernate Interceptor中进行,在Spring DB Configuration的时候注册.

If you want to modify the query after the prepared statement, you can do it into an Hibernate Interceptor, and register it during Spring DB Configuration.

您的 MyInterceptor 应该实现 Hibernate Interceptor.class 或扩展 EmptyInterceptor.class.您正在寻找的方法是:

Your MyInterceptor either should implements Hibernate Interceptor.class or extends EmptyInterceptor.class. The method you are looking for is:

public String onPrepareStatement(String sql);

如果您需要更多东西,可以尝试使用 Event Listeners,但我不确定是否有适合您的情况.

If you need something more, you could give a try with the Event Listeners, but I am not sure there is one for your case.

这里有一些文档:

Hibernate 4.0 拦截器和事件如何将拦截器与 Spring 集成

这篇关于Hibernate 和 Spring 在提交到 DB 之前修改查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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