Tengo entidades que persisten en MongoDB y uso Spring Data MongoRepository para obtener datos. Ahora quiero aplicar el filtro a todas las consultas que se ejecutaron en las entidades, así que decidí usar el filtro de hibernación, algo como esto:
@Entity @QueryEntity @Document(collection = "Opportunity") @NoArgsConstructor @AllArgsConstructor @Getter @Setter @CompoundIndexes({ @CompoundIndex(name = "productGroup_userId_uniqueness", def = "{'productGroupCode' : 1, 'userId': 1}", unique = true) }) @FilterDef(name = "defaultFilter",parameters = @ParamDef(name = "unitCode",type = "string")) @Filter(name = "defaultFilter" , condition = " unitCode like :unitCode") public class Opportunity { @Id @Indexed private String id; @Indexed @Enumerated(EnumType.STRING) private OpportunityStatus opportunityStatus = OpportunityStatus.OPEN; private LeadType leadType; @Indexed private String userId; @Indexed private String productCode; @Indexed private String productGroupCode; @Indexed private Long actionId; private String assigneeId; @Transient private List<AbstractCommand> commandHistory = new ArrayList<>(); @Transient private Map<Long, Boolean> actionStatus = new HashMap<>(); private String unitCode; }y esta es la clase de repositorio:
@Repository public interface OpportunityRepository extends MongoRepository<Opportunity, String>, QuerydslPredicateExecutor<Opportunity> { // this repository contains more than 20 methods // and all of theme removed for question brevity }Y habilité el filtro de hibernación en la sesión de esta manera:
Session session = (entityManager).unwrap(Session.class); session.enableFilter(filterName).setParameter("unitCode", this.getCurrentUserUnitCode()); Ahora, cuando llamo a OpportunityRepository.findAll(Predicate predicate, Pageable pageable) esperaba aplicar el filtro definido en la entidad, pero no funcionó.
Creo que la razón es que MongoRepository no tiene ningún sentido de hibernación @Filter y debería usar otra forma de agregar la cláusula where a todas las consultas de mongo que se ejecutan en la entidad de Opportunity .