Guevara 29 de out. de 2010
Tentei com if’s, e não me retorna o imóvel cadastrado se buscar pela cidade e estado corretos:
public List < Imovel > busca ( Imovel imovel , BigDecimal valorMin , BigDecimal valorMax ) {
Criteria crit = session .createCriteria ( Imovel .class )
.createAlias ( "endereco" , "e" ) ;
Criterion val = Restrictions .between ( "valor" , valorMin , valorMax ) ;
Criterion qua = Restrictions .eq ( "quartos" , imovel .getQuartos ()) ;
Criterion bai = Restrictions .ilike ( "e.bairro" , imovel .getEndereco () .getBairro () , MatchMode .ANYWHERE ) ;
Criterion cid = Restrictions .ilike ( "e.cidade" , imovel .getEndereco () .getCidade () , MatchMode .ANYWHERE ) ;
Criterion est = Restrictions .ilike ( "e.estado" , imovel .getEndereco () .getEstado () , MatchMode .ANYWHERE ) ;
Conjunction conjunction = Restrictions .conjunction () ;
if ( imovel .getQuartos () != null && ! imovel .getQuartos () .equals ( null ))
conjunction .add ( qua ) ;
if ( imovel .getEndereco () .getBairro () != null && ! imovel .getEndereco () .getBairro () .isEmpty ())
conjunction .add ( bai ) ;
if ( imovel .getEndereco () .getCidade () != null && ! imovel .getEndereco () .getCidade () .isEmpty ())
conjunction .add ( cid ) ;
if ( imovel .getEndereco () .getEstado () != null && ! imovel .getEndereco () .getEstado () .isEmpty ())
conjunction .add ( est ) ;
if ( valorMin != null && valorMax != null )
conjunction .add ( val ) ;
crit .add ( conjunction ) ;
return crit .list () ;
}
Na stacktrace está td normal, apenas não retorna os imóveis de acordo com o critério de pesquisa.
Guevara 29 de out. de 2010
Testei o método em dois bancos, Postgre e MySQL, a Criteria não retorna o imóvel cadastrado:
Pesquisando por “barra”, quero que ele liste o imóvel no bairro Barra da Tijuca:
Código:
public List < Imovel > busca ( final Imovel imovel , BigDecimal valorMin , BigDecimal valorMax ) {
Criteria crit = session.createCriteria(Imovel.class).createAlias("endereco", "e") ;
if (imovel.getQuartos() != null && !imovel.getQuartos().equals(null))
crit.add(Restrictions.eq("quartos", imovel.getQuartos())) ;
if (imovel.getEndereco().getBairro() != null && !imovel.getEndereco().getBairro().isEmpty()) ;
crit.add(Restrictions.ilike("e.bairro", imovel.getEndereco().getBairro(), MatchMode.ANYWHERE)) ;
if (imovel.getEndereco().getCidade() != null && !imovel.getEndereco().getCidade().isEmpty()) ;
crit.add(Restrictions.ilike("e.cidade", imovel.getEndereco().getCidade(), MatchMode.ANYWHERE)) ;
if (imovel.getEndereco().getEstado() != null && !imovel.getEndereco().getEstado().isEmpty()) ;
crit.add(Restrictions.ilike("e.estado", imovel.getEndereco().getEstado(), MatchMode.ANYWHERE)) ;
if (valorMin != null && valorMax != null)
crit.add(Restrictions.between("valor", valorMin, valorMax)) ;
List<Imovel> imoveis = crit.list() ;
System.out.println("Quantidade retornada : " + imoveis . size ());
return crit.list() ;
}
Stacktrace:
Minha conclusão é que só pode ser bug do Hibernate!
Abraço!
Guevara 30 de out. de 2010
Baixei o Hibernate 3.6.0-final e voltou a funcionar esta bagaça…