0

Preciso implementar uma lógica de consulta dinâmica, no BD usando JPQL, sendo que tenho 4 filtros:

1 - COORDENADOR DO PROJETO
2 - FORNECEDOR DO SERVIÇO
3 - DATA INICIO DO PROJETO
4 - DATA FINAL DO PROJETO

Qualquer um dos campos pode ser combinados entre si, isto é, pode ser selecionado COORDENADOR e DATA INICIAL, FORNECEDOR e DATA INICIAL, e assim por diante...

As tabelas estão sendo gerenciadas pelos respectivos Entities:

ProjetoEntity contem o attributo "coordenador"
PlanoEntregaEntity contem o atributo "fornecedor"
EntregaEntity contem o atributo "datainicial" e "datafinal".

Tentei implementar Criteria (uaihebert, easycriteria, criteriabuilder e etc), mas não tive muito sucesso, se alguém tiver alguma indicação, mesmo wue inicial, ficarei grato.

1
  • Mas essa pesquisa vai retornar uma lista de qual entidade (ProjetoEntity, PlanoEntregaEntity ou EntregaEntity)? 23/12/2016 às 17:18

1 Resposta 1

1

Vou deixar uma idéia que você pode tentar aprimorar. O hibernate possui um recurso que permite criar uma consulta a partir de uma instância: org.hibernate.criterion.Example. Como você já possui as entidades, basta setar os valores que você quer pesquisar na própria entidade e criar uma função para recebê-la como parâmetro e retornar um Criterion para usar nas consultas. Segue o exemplo abaixo:

public Criterion criarCriteria(Object projeto) {
    Criterion retorno = null;
    if (projeto != null) {
        retorno = Example.create(projeto)
                        .ignoreCase()
                        .enableLike(MatchMode.ANYWHERE);
    }
    return retorno;
}

@SuppressWarnings("unchecked")
public List<ProjetoEntity> consultarProjeto(ProjetoEntity projeto, PlanoEntregaEntity planoEntrega, EntregaEntity entrega) {
    DetachedCriteria criteria = DetachedCriteria.forClass(ProjetoEntity.class);

    Criterion criterionProjeto = this.criarCriteria(projeto);
    if (criterionProjeto != null) {
        criteria.add(criterionProjeto);
    }

    Criterion criterionPlanoEntrega = this.criarCriteria(planoEntrega);
    if (criterionPlanoEntrega != null) {
        DetachedCriteria criteriaPlanoEntrega = DetachedCriteria.forClass(PlanoEntregaEntity.class);
        criteriaPlanoEntrega.add(criterionPlanoEntrega);
        criteriaPlanoEntrega.setProjection(Projections.property("id"));
        criteria.add(Property.forName("planoEntrega").in(criteriaPlanoEntrega));
    }

    Criterion criterionEntrega = this.criarCriteria(entrega);
    if (criterionEntrega != null) {
        DetachedCriteria criteriaEntrega = DetachedCriteria.forClass(EntregaEntity.class);
        criteriaEntrega.add(criterionEntrega);
        criteriaEntrega.setProjection(Projections.property("id"));
        criteria.add(Property.forName("entrega").in(criteriaEntrega));
    }

    return criteria.getExecutableCriteria(this.getSession()).list();
}

Você deve fazer log-in para responder a esta pergunta.

Esta não é a resposta que você está procurando? Pesquise outras perguntas com a tag .