Paulo Galdo Sandoval
Paulo Galdo Sandoval

Reputation: 2203

Exclude value using Hibernate

i'm trying to exclude one value from a column on my results.

i've tried using conjunction() and and() operators but cant reach what i want.

this is my last attemp to filter:

public List<Factura> getAll() {
    Criteria criteria = getSession().createCriteria(Factura.class);
    Criterion criterion = Restrictions.conjunction().add(Restrictions.eq("estado", "INICIADO"))
                                                    .add(Restrictions.eq("estado", "CONFIRMADO"));
    criteria.addOrder(Order.desc("idFactura"));
    criteria.add(criterion);
    List<Factura> list = criteria.list();
    return list;
}

in my table in the column estado i have 3 possible values. INICIADO, CONFIRMADO, RESERVADO i want to exclude from my results RESERVADO but i don't know what i'm doing wrong, only get the full list without filtering.

Upvotes: 0

Views: 653

Answers (2)

Sergii Lagutin
Sergii Lagutin

Reputation: 10681

Use Restrictions.ne method

public List<Factura> getAll() {
    Criteria criteria = getSession().createCriteria(Factura.class);
    criteria.add(Restrictions.ne("estado", "RESERVADO"));
    criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
    return criteria.list();
}

Upvotes: 1

Paulo Galdo Sandoval
Paulo Galdo Sandoval

Reputation: 2203

Ok i find a solution after try a lot of things and it's very easy to understand.

public List<Factura> getAll() {
    Criteria criteria = getSession().createCriteria(Factura.class);
    criteria.add(
            Restrictions.not(
                    Restrictions.in("estado", new String[]{"RESERVADO"})));
    criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
    List<Factura> list = criteria.list();
    return list;
}

with this i can exclude any value that i want.

Thanks all for reading and sorry for waste your time.

Upvotes: 1

Related Questions