Using a Criteria NOT with Hibernate

-1

Currently, I have a search with Criteria that brings me results normally, but I want to add an implementation of type NOT to it.

Does anyone have an idea how I could do this?

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

Here's 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;
    
asked by anonymous 27.07.2018 / 23:49

1 answer

1

Try 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 .

Oh, and do not use things like new Integer and new Long . It is unnecessary since autoboxing already does this for you with the advantage of still keeping some values cache. Even in Java 9, these constructors of the packager classes were marked with @Deprecated for this reason.

    
28.07.2018 / 07:49