The final goal that I want to achieve is to use some information from the request headers to use on a Hibernate filter.
The webapp is a fairly neat Spring boot webapp using Jpa Repositories.
I've configured a filter definition in the model entity and I want to have it activated on the webapp. I use a request handler:
The Entity:
@Entity
@Getter
@Setter
@Table(name = "personas")
@FilterDef(name = "filtro_personas", parameters = @ParamDef(name = "idparam", type = "int"))
public class Persona implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE)
@Filter(name = "filtro_personas", condition = ":idparam = id")
private Integer id;
private String nombre;
@OneToMany(mappedBy ="persona")
private List<Wallet> cuentas;
}
The request handler:
public class LangHeaderInterceptor implements HandlerInterceptor {
private EntityManager entityManager;
public LangHeaderInterceptor( EntityManager entityManager) {
this.entityManager = entityManager;
}
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler){
if (entityManager != null ) {
// Sample of use of the filter.at this point the HttpServletRequest and entity manager are filled.
Session s = entityManager.unwrap(Session.class);
s.enableFilter("filtro_personas").setParameter("idparam", 1).;
}
return true;
}
}
The request handler configuration:
@Configuration
public class WebConfigurer implements WebMvcConfigurer {
@Autowired
private EntityManager entityManager;
@Override
public void addInterceptors(InterceptorRegistry registry) {
WebMvcConfigurer.super.addInterceptors(registry);
//System.out.println("TRACING MESSAGE: CUSTOM INTERCEPTOR ADDED");
registry.addInterceptor(new LangHeaderInterceptor(entityManager));
}
}