Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

112
Visualizações
How to append where clause to all queries that run with spring data MongoRepository?

I have entities that are persisted in MongoDB and use spring data MongoRepository to fetch data. Now i want to apply filter to all queries that executed on the entites, so i decided to use hibernate filter, something like this:

@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;

}

and this is the repository class:

@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
}

And I enabled hibernate filter on session with this way:

Session session = (entityManager).unwrap(Session.class);
session.enableFilter(filterName).setParameter("unitCode", this.getCurrentUserUnitCode());

Now, when I call OpportunityRepository.findAll(Predicate predicate, Pageable pageable) i expected to apply the defined filter on the entity, but it didn't work.

I think the reason is that MongoRepository hasn't any sense of hibernate @Filter and i should use another way to append where clause to all mongo queries that running on the Opportunity entity.

over 4 years ago · Santiago Trujillo
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda