问题是,我有一个Hibernate阻断器,如下所示:
public class CustomInterceptor extends EmptyInterceptor {
private String tenant;
public CustomInterceptor(String tenant) {
this.tenant = tenant;
}
@Override
public String onPrepareStatement(String sql) {
String prepedStatement = super.onPrepareStatement(sql);
if (tenant != null) {
prepedStatement = prepedStatement.replaceAll("TABLE_NAME_1", "TABLE_" + tenant);
}
return prepedStatement;
} }我可以在引导上述拦截器期间初始化,但我想要的是能够向不同的租户注册相同的拦截器,就像Spring拦截器允许的一样,如下所示:
registry.addInterceptor(new CustomInterceptor("tenant1")).addPathPatterns("/wow/tenant1");
registry.addInterceptor(new CustomInterceptor("tenant2")).addPathPatterns("/wow/tenant2");
registry.addInterceptor(new CustomInterceptor("tenant3")).addPathPatterns("/wow/tenant3");我无法在Hibernate中对多个拦截器进行注册,而且我也不可能使用Spring,因为在我的示例中,Spring没有提供Hibernate拦截器所做的事情(即onPrepareStatement能够在运行时更改表名)。
,谁能建议一下如何用Hibernate注册多个拦截器?我不确定Hibernate.是否可行。
编辑:
答案(基于我的研究和实现):在引导时注册多个拦截器,然后根据传入的请求模式定向到不同的拦截器是Spring提供的,Hibernate不支持的。
发布于 2018-01-18 08:56:41
您可以引入一个InterceptorProxy来保存CustomInterceptors列表。
public class InterceptorProxy extends EmptyInterceptor {
private List<CustomInterceptor> interceptors;
public InterceptorProxy (List<CustomInterceptor>) {
this.interceptors = interceptors;
}
@Override
public String onPrepareStatement(String sql) {
String prepedStatement = super.onPrepareStatement(sql);
for (CustomInterceptor ci:interceptors) {
//your logic here
}
return prepedStatement;
}
}https://stackoverflow.com/questions/48315630
复制相似问题