Using a NOT Criteria with Hibernate

Asked

Viewed 46 times

-1

Currently, I have a survey with Criteria that brings me results normally, but I want to add an implementation like NOT.

Does anyone have any idea how I could do that?

I put in the code a comment with a possible idea of the implementation, but did not succeed.

Follow the code part:

@SuppressWarnings({ "unchecked" })
public List<ContaCorrenteModel> findByContaClieAssinat(Short banco, Short agencia, Long conta) {

    List<ContaCorrenteModel> ccModel;



public List<ContaCorrenteModel> findByContaClieAssinat(Short banco, Short agencia, Long conta) {

    List<ContaCorrenteModel> ccModel;



ArquivoLog.escreverLog
("Iniciando consulta [ClienteDaoImpl] findByContaClieAssinat - Parametros utilizados:"
 + " Banco " + banco +" Agencia " + agencia + " Conta " + conta );
    this.session = ConexaoHibert.getInstance();

    switch (Principal.getBase()) {
    case CENTRAL:
        List<TbCntaCrrtClie> tbCntaCrrtClieList = new ArrayList<TbCntaCrrtClie>();


        Criteria crit = this.session.createCriteria(TbCntaCrrtClie.class, "conta");

        crit.add(Restrictions.eq("conta.cdBanc", banco));
        crit.add(Restrictions.eq("conta.cdAgen", agencia));

   // Restricao que tenho que Adicionar a consulta criteria //
   crit.add(Restrictions.not("conta.nrOrdeTitl", new Long(2) ));

        crit.setFetchMode("nrSequPessUnic", FetchMode.JOIN);
        crit.setFetchMode("tbPenumUnic.nrSequClieFirmPodr", FetchMode.JOIN);
        crit.setFetchMode("tbPenumUnic.tbRpreGrupCollection", FetchMode.JOIN);
        crit.setFetchMode("nrSequNatzCnta", FetchMode.JOIN);



        tbCntaCrrtClieList = crit.list();

        ArquivoLog.escreverLog("Total de clientes CENTRAL: " + tbCntaCrrtClieList.size());

        ccModel =  ContaCorrenteModelAdapter.adapt(tbCntaCrrtClieList);
        break;

1 answer

1

Try to use this:

crit.add(Restrictions.ne("conta.nrOrdeTitl", 2L));

The name of the method ne means not equals, that is, it is the opposite of eq which means equals.

Ah, and don’t use things like new Integer and new Long. They are unnecessary since autoboxing already does this for you with the advantage of still keeping cache of some values. Even in Java 9, these package class constructors were marked with @Deprecated for this reason.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.