我正在撰写一份多元化的应用程序。我们跟随用户实体的地方:
@Entity
@FilterDef(name="byTenantIdFilter", parameters={
@ParamDef( name="tenantId", type="long")
})
@Filter(name = "byTenantIdFilter", condition = ":tenantId = tenant_id")
@Table(name = "USER_ACCOUNT")
public class User {
@Column(name = "USER_ID")
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
long id;
@Column(name = "LOGIN_ID", unique = true)
String loginId;
为了启用过滤器我已经编写了一个获取会话的拦截器,启用了相关的过滤器并设置了参数tenantId。
以下是拦截器的代码:
public class FilterEnablerInterceptor implements HandlerInterceptor {
@Autowired
private EntityManagerFactory entityManagerFactory;
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
throws Exception {
AuthenticatedUser user = (AuthenticatedUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
SessionFactory sessionFactory = entityManagerFactory.unwrap(SessionFactory.class);
Session session = sessionFactory.openSession();
session.enableFilter("byTenantIdFilter").setParameter("tenantId", user.getTenantId());
return true;
}
但是,过滤器查询不会附加到正在执行的最终查询中。可能是因为另一个会话用于获取结果。
我怎样才能完成这项工作?