Search code examples
javajpacriteriacriteria-api

Criteria API: Using the Criteria API to Create Queries documentation


I've a very simple question about this documentation Using the Criteria API to Create Queries: documentation

Here in the Expression Methods in the CriteriaBuilder Interface part you could read the following regarding and method in this part: Table 35-3 Compound Predicate Methods in the CriteriaBuilder Interface and method usage example:

CriteriaQuery<Pet> cq = cb.createQuery(Pet.class);
Root<Pet> pet = cq.from(Pet.class);
cq.where(cb.equal(pet.get(Pet_.name), "Fido")
    .and(cb.equal(pet.get(Pet_.color), "brown"))

But actually this is not working because here after the where method you could not call .and()... The proper usage is:

CriteriaQuery<BookEntity> cq = cb.createQuery(Pet.class);
Root<Pet> pet = cq.from(Pet.class); 
cq.where(cb.and(cb.equal(pet.get(Pet_.name), "Fido"), 
                cb.equal(pet.get(Pet_.color), "brown")));

So why is this? Maybe there are some version difference? Or maybe it is just some error in the documentation but in case of that I will leave this for a good example for usage.


Solution

  • The Oracle "tutorial" has various errors, that being but one.

    The definitive guide to what is possible with JPA Criteria is the JPA spec and the javadoc. Other than that make use of a vendors own documentation since they should all cover the standard handling (as well as their own extensions, hopefully making clear what are their own extensions). One such example is this one from DataNucleus JPA.