Hibernate和Spring修改查询,然后提交给DB [英] Hibernate and Spring modify query Before Submitting to DB

查看:54
本文介绍了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.

推荐答案

如果要在准备好的语句后修改查询,可以将其放入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);

如果您还需要其他东西,可以尝试使用事件监听器,但是我不确定您的情况是否合适.

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天全站免登陆