SQL语句拦截器类中不推荐使用错误“org.hibernate.EmptyInterceptor "

tpgth1q7  于 2023-10-23  发布在  其他
关注(0)|答案(1)|浏览(111)

我迁移到了spring Boot 3,我的SQL语句拦截器给出了一个错误:'org.hibernate.EmptyInterceptor' is deprecated.我不知道怎么解决。
下面是我的代码:

import org.hibernate.EmptyInterceptor;
import org.slf4j.MDC;
import org.springframework.util.StringUtils;

public class Interceptor extends EmptyInterceptor {

    @Override
    public String onPrepareStatement(String sql) {
        if (StringUtils.hasLength(sql) && sql.toLowerCase().startsWith("select")) {
            final String entityName = sql.substring(7, sql.indexOf("."));
            final String idEntreprise = MDC.get("idEntreprise");

            if (StringUtils.hasLength(entityName)
                    && !entityName.toLowerCase().contains("entreprise")
                    && !entityName.toLowerCase().contains("roles")
                    && StringUtils.hasLength(idEntreprise)) {

                if (sql.contains("where")) {
                    sql = sql + " and "+entityName+".idEntreprise = "+idEntreprise;
                } else {
                    sql = sql + " where "+entityName+".idEntreprise = "+idEntreprise;
                }

      }
            }
            return super.onPrepareStatement(sql);
        }
    }

在我的代码中,我用Interceptor类替换了EmptyInterceptor,但什么也没有。

u4vypkhs

u4vypkhs1#

尝试实现StatementInspector并覆盖inspect(String sql)方法

public class Interceptor implements StatementInspector {
 //Use this method instead of onPrepareStatement
    @Override
    public String inspect(String sql) {
        ...
    }
}

https://docs.jboss.org/hibernate/orm/6.0/javadocs/org/hibernate/resource/jdbc/spi/StatementInspector.html

相关问题