Daniel Brito
Daniel Brito

Reputation: 113

How to make a sql in JPA?

How to make a sql SELECT count(*) from item_entrada where not isfinalizado with JPA ?

`isfinalizado boolean DEFAULT false`

rows:

true true true false

my expect result is 1 but i receive 3

my code:

        CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
        Root<ItensEntrada> rt = cq.from(ItensEntrada.class);
        cq.select(em.getCriteriaBuilder().count(rt));
        cq.where(rt.get("isfinalizado"));
        Query q = em.createQuery(cq);
        return ((Long) q.getSingleResult()).intValue();

Upvotes: 0

Views: 43

Answers (1)

Abhinav
Abhinav

Reputation: 26

In SQL you are using not isfinalizado while in criteria query you are not checking for not condition.

I will suggest to use predicate in criteria query to achieve that.

Upvotes: 1

Related Questions