Skip to main content
Formatação
Fonte Link
Victor Stafusa
  • 64,9mil
  • 9
  • 120
  • 189

Interface: public interface TagsDayRepositoryQuery {

public interface TagsDayRepositoryQuery {
    
    public List<TagsDay> filtrar(List<Long> ids, TagsDayFilter tagsDayFilter); 

}

}

e a classe de implementação: public class TagsDayRepositoryImpl implements TagsDayRepositoryQuery{

public class TagsDayRepositoryImpl implements TagsDayRepositoryQuery{

    @PersistenceContext
    private EntityManager manager;

    @Override
    public List<TagsDay> filtrar(List<Long> ids, TagsDayFilter tagsDayFilter) {
        
        CriteriaBuilder builder = manager.getCriteriaBuilder();
        CriteriaQuery<TagsDay> criteria = builder.createQuery(TagsDay.class);
        
        Root<TagsDay> root = criteria.from(TagsDay.class);
        
        //criar as restrições
        Predicate[] predicates = criarRestricoes(ids, tagsDayFilter, builder, root);
        criteria.where(predicates);
        
        TypedQuery<TagsDay> query = manager.createQuery(criteria);
        return query.getResultList();
    }

    private Predicate[] criarRestricoes(List<Long> ids, TagsDayFilter tagsDayFilter, CriteriaBuilder builder, Root<TagsDay> root) {
        
        List<Predicate> predicates = new ArrayList<>();     
        
        if(ids.size() > 0) {
            
            for(int i = 0; i < ids.size(); i++) {
                predicates.add(builder.equal(root.get("id"), ids.get(i)));  //adiciona vários ids para consulta         
            }
            

        }
        if(tagsDayFilter.getDataInicial() != null) {
            predicates.add(builder.greaterThanOrEqualTo(root.get("data_coleta"), tagsDayFilter.getDataInicial()));
        }
        if(tagsDayFilter.getDataFinal() != null) {
            predicates.add(builder.lessThanOrEqualTo(root.get("data_coleta"), tagsDayFilter.getDataFinal()));
        }
        
        return predicates.toArray(new Predicate[predicates.size()]);
    }

    

}

}

e na URL faço: localhost:8080/tagsday/search?ids=1,2&dataInicial=2018-09-27&dataFinal=2018-09-28 localhost:8080/tagsday/search?ids=1,2&dataInicial=2018-09-27&dataFinal=2018-09-28

Interface: public interface TagsDayRepositoryQuery {

public List<TagsDay> filtrar(List<Long> ids, TagsDayFilter tagsDayFilter);

}

e a classe de implementação: public class TagsDayRepositoryImpl implements TagsDayRepositoryQuery{

@PersistenceContext
private EntityManager manager;

@Override
public List<TagsDay> filtrar(List<Long> ids, TagsDayFilter tagsDayFilter) {
    
    CriteriaBuilder builder = manager.getCriteriaBuilder();
    CriteriaQuery<TagsDay> criteria = builder.createQuery(TagsDay.class);
    
    Root<TagsDay> root = criteria.from(TagsDay.class);
    
    //criar as restrições
    Predicate[] predicates = criarRestricoes(ids, tagsDayFilter, builder, root);
    criteria.where(predicates);
    
    TypedQuery<TagsDay> query = manager.createQuery(criteria);
    return query.getResultList();
}

private Predicate[] criarRestricoes(List<Long> ids, TagsDayFilter tagsDayFilter, CriteriaBuilder builder, Root<TagsDay> root) {
    
    List<Predicate> predicates = new ArrayList<>();     
    
    if(ids.size() > 0) {
        
        for(int i = 0; i < ids.size(); i++) {
            predicates.add(builder.equal(root.get("id"), ids.get(i)));  //adiciona vários ids para consulta         
        }
        

    }
    if(tagsDayFilter.getDataInicial() != null) {
        predicates.add(builder.greaterThanOrEqualTo(root.get("data_coleta"), tagsDayFilter.getDataInicial()));
    }
    if(tagsDayFilter.getDataFinal() != null) {
        predicates.add(builder.lessThanOrEqualTo(root.get("data_coleta"), tagsDayFilter.getDataFinal()));
    }
    
    return predicates.toArray(new Predicate[predicates.size()]);
}

}

e na URL faço: localhost:8080/tagsday/search?ids=1,2&dataInicial=2018-09-27&dataFinal=2018-09-28

Interface:

public interface TagsDayRepositoryQuery {
    
    public List<TagsDay> filtrar(List<Long> ids, TagsDayFilter tagsDayFilter); 

}

e a classe de implementação:

public class TagsDayRepositoryImpl implements TagsDayRepositoryQuery{

    @PersistenceContext
    private EntityManager manager;

    @Override
    public List<TagsDay> filtrar(List<Long> ids, TagsDayFilter tagsDayFilter) {
        
        CriteriaBuilder builder = manager.getCriteriaBuilder();
        CriteriaQuery<TagsDay> criteria = builder.createQuery(TagsDay.class);
        
        Root<TagsDay> root = criteria.from(TagsDay.class);
        
        //criar as restrições
        Predicate[] predicates = criarRestricoes(ids, tagsDayFilter, builder, root);
        criteria.where(predicates);
        
        TypedQuery<TagsDay> query = manager.createQuery(criteria);
        return query.getResultList();
    }

    private Predicate[] criarRestricoes(List<Long> ids, TagsDayFilter tagsDayFilter, CriteriaBuilder builder, Root<TagsDay> root) {
        
        List<Predicate> predicates = new ArrayList<>();     
        
        if(ids.size() > 0) {
            
            for(int i = 0; i < ids.size(); i++) {
                predicates.add(builder.equal(root.get("id"), ids.get(i)));  //adiciona vários ids para consulta         
            }
            

        }
        if(tagsDayFilter.getDataInicial() != null) {
            predicates.add(builder.greaterThanOrEqualTo(root.get("data_coleta"), tagsDayFilter.getDataInicial()));
        }
        if(tagsDayFilter.getDataFinal() != null) {
            predicates.add(builder.lessThanOrEqualTo(root.get("data_coleta"), tagsDayFilter.getDataFinal()));
        }
        
        return predicates.toArray(new Predicate[predicates.size()]);
    }

    

}

e na URL faço: localhost:8080/tagsday/search?ids=1,2&dataInicial=2018-09-27&dataFinal=2018-09-28

Fonte Link

Consulta com vários Ids e data com JPA e Spring via RESTApi

Bom dia, estou tentando montar um consulta com Spring JPA e Rest onde posso inserir diversos IDs e datainicial e datafinal. A consulta funciona se eu colocar somente um ID com as datas. O código está dessa forma:

@GetMapping("/search")
public List<TagsDay> pesquisar(@RequestParam(value = "ids") List<Long> ids, TagsDayFilter tagsDayFilter){
    return tagsDayRepository.filtrar(ids, tagsDayFilter);
}

Interface: public interface TagsDayRepositoryQuery {

public List<TagsDay> filtrar(List<Long> ids, TagsDayFilter tagsDayFilter);

}

e a classe de implementação: public class TagsDayRepositoryImpl implements TagsDayRepositoryQuery{

@PersistenceContext
private EntityManager manager;

@Override
public List<TagsDay> filtrar(List<Long> ids, TagsDayFilter tagsDayFilter) {
    
    CriteriaBuilder builder = manager.getCriteriaBuilder();
    CriteriaQuery<TagsDay> criteria = builder.createQuery(TagsDay.class);
    
    Root<TagsDay> root = criteria.from(TagsDay.class);
    
    //criar as restrições
    Predicate[] predicates = criarRestricoes(ids, tagsDayFilter, builder, root);
    criteria.where(predicates);
    
    TypedQuery<TagsDay> query = manager.createQuery(criteria);
    return query.getResultList();
}

private Predicate[] criarRestricoes(List<Long> ids, TagsDayFilter tagsDayFilter, CriteriaBuilder builder, Root<TagsDay> root) {
    
    List<Predicate> predicates = new ArrayList<>();     
    
    if(ids.size() > 0) {
        
        for(int i = 0; i < ids.size(); i++) {
            predicates.add(builder.equal(root.get("id"), ids.get(i)));  //adiciona vários ids para consulta         
        }
        

    }
    if(tagsDayFilter.getDataInicial() != null) {
        predicates.add(builder.greaterThanOrEqualTo(root.get("data_coleta"), tagsDayFilter.getDataInicial()));
    }
    if(tagsDayFilter.getDataFinal() != null) {
        predicates.add(builder.lessThanOrEqualTo(root.get("data_coleta"), tagsDayFilter.getDataFinal()));
    }
    
    return predicates.toArray(new Predicate[predicates.size()]);
}

}

e na URL faço: localhost:8080/tagsday/search?ids=1,2&dataInicial=2018-09-27&dataFinal=2018-09-28