Search code examples
jpa-2.0criteriaquery

How do I dynamically create CriteriaQuery


I want to create equivalent CriteriaQuery to this query : select u from User u where u.name = "John" and u.surname = "Black" and u.middlename = "Small"; but I have problem with "where" predicate. Thanks


Solution

  • You can use:

    
    CriteriaBuilder builder = entityManager.getCriteriaBuilder();
    CriteriaQuery c = ...;
    Root root = ...;
    
    Predications name = builder.equal(root.get("name"), name);
    Predications surname = ...;
    Predications middlename = ...;
    
    c.where(name, surname, middlename);