Criteria Hibernate ilike com filtros

4 respostas
tmvolpato

Bom dia,

Estou com uma dúvida o código abaixo está funcionando em partes

KeyName : campo aonde coloco qualquer nome e mando buscar no banco
job : Classe de Enum com alguns cargos (Gerente, Analista)...

Problema: No campo keyName qdo eu coloco um nome que tenha no banco ele não traz nada e
qdo coloco o nome e seleciono o job dai ele faz o filtro pelo nome que eu coloquei - excelente até ai

Pergunta : tem como o ilike fazer a busca sem eu ter que selecionar o job junto?

@Override
public List<Employee> getListBasic(String keyName, Job keyJob) {
Criterion[] filters = new Criterion[2];
if (keyName != null){
filters[0] = Restrictions.ilike(name,  keyName , MatchMode.ANYWHERE);
}
if (keyJob != null) {
filters[1] = Restrictions.eq(job, keyJob);
}


return getListBasic(Employee.class, null, filters);
}

4 Respostas

XOOM

Se eu entendi certo, pq não daria?

Fonte: http://docs.jboss.org/hibernate/orm/3.3/reference/en/html/querycriteria.html

tmvolpato

se eu fizer isso dá,

mas estou passando outro campo embaixo
dai a saida fica assim

from EMPLOYEE this_ inner join PERSON this_1_ on this_.id=this_1_.id left outer join DEPARTMENT department2_ on this_.DEPARTMENT_ID=department2_.ID where this_.NAME ilike ? and this_.JOB=?

está saindo AND está funcionando como filtro, até ai tudo bem
eu vi que tenho que fazer um ou “OR”

to tentando aplicar o or de forma que eu não tenha que mudar o jeito que está
essa lista

se alguem souber, dá uma ajuda ai

XOOM

Então, você disse que queria efetuar a busca sem necessidade de selecionar o Job. O seu select deveria ficar assim

from EMPLOYEE this_ inner join PERSON this_1_ on this_.id=this_1_.id left outer join DEPARTMENT department2_ on this_.DEPARTMENT_ID=department2_.ID where this_.NAME ilike ?
Ou seja vc vai passar o Job = Null… correto? Se você estiver pensando em utilizar a condição OR entre KeyName e Job, ficando:


from
EMPLOYEE this_
inner join
PERSON this_1_
on this_.id=this_1_.id
left outer join
DEPARTMENT department2_
on this_.DEPARTMENT_ID=department2_.ID
where
this_.NAME ilike ?
OR this_.JOB=?

Independente se Name ou Job True ou False, irá trazer resultado, só não irá trazer caso ambos sejam False.

tmvolpato

é isso que eu estou tentando fazer

qro ele funcionando com o AND e OR

Criado 18 de julho de 2012
Ultima resposta 18 de jul. de 2012
Respostas 4
Participantes 2