1

Hi I am new to JPA Criteria builder concept, I am using PostgreSQL and JPA Query.I got this query SELECT id, full_name, email FROM nurses WHERE(lower(sender) LIKE '%bar%' and lower(receiver) LIKE '%bar%').but how to convert it to JPA Criteria builder.

 entityManager.getTransaction().begin();
            CriteriaBuilder cb = entityManager.getCriteriaBuilder();
            System.out.println(entityManager + " Conected");
            CriteriaQuery<sourceTracking> cq = cb.createQuery(sourceTracking.class);
            Root<sourceTracking> data1 = cq.from(sourceTracking.class);
            cq.where(cb.like(data1.<String>get("sender"), "%"+sender+"%"),cb.like(data1.<String>get("receiver"), "%"+receiver+"%");
            cq.select(data1);
            TypedQuery<sourceTracking> tquery = entityManager.createQuery(cq);
            sourselList = tquery.getResultList();

1 Answer 1

2

CriteriaBuilder.lower() will do what you want.

e.g.

cb.like(cb.lower(data1.get("sender")), "%" + sender + "%")

or, to make your pattern lowercase too:

cb.like(cb.lower(data1.get("sender")), cb.lower(cb.literal("%" + sender + "%")))
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.