Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

106
Views
¿Cómo agregar la cláusula where a todas las consultas que se ejecutan con Spring Data MongoRepository?

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 .

over 4 years ago · Santiago Trujillo
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!