Skip to content

Criteria API

Imagine you're in a library, searching for a specific book among thousands of titles. Now, think of a database as a giant library, with tons of information stored in it. The Criteria API is like your super-smart librarian, helping you find exactly what you need without getting lost in all those shelves.

In simple terms, the Criteria API is a tool used in programming to make querying databases easier and more flexible. Instead of writing complex SQL queries (which can feel like deciphering hieroglyphics sometimes), you can use the Criteria API to construct your queries using intuitive methods and conditions.

It's like having a conversation with your database. You tell it what you're looking for, using familiar terms and conditions, and it fetches the data you need. So, whether you're searching for specific books in a library or filtering through heaps of data in a database, the Criteria API is your friendly guide, making the process smooth and straightforward.

java
// Let's create a CriteriaBuilder to start our query
CriteriaBuilder builder = entityManager.getCriteriaBuilder();

// Create a CriteriaQuery for the Book entity
CriteriaQuery<Book> criteriaQuery = builder.createQuery(Book.class);

// Specify the root entity (in this case, Book)
Root<Book> bookRoot = criteriaQuery.from(Book.class);

// Define the conditions for our query
criteriaQuery.select(bookRoot)
    .where(
        builder.equal(bookRoot.get("genre"), "Science Fiction"),  // Looking for science fiction books
        builder.greaterThan(bookRoot.get("publicationYear"), 2000) // Published after 2000
    );

// Execute the query and get the result
List<Book> sciFiBooksAfter2000 = entityManager.createQuery(criteriaQuery).getResultList();

In this example, we're using the Criteria API to construct a query for science fiction books published after the year 2000. It's like telling our librarian, "Hey, can you find me all the science fiction books that came out after 2000?" And the Criteria API takes care of translating that request into a database query, giving us the result we need.

java
// Let's create a CriteriaBuilder to start our query
CriteriaBuilder builder = entityManager.getCriteriaBuilder();

// Create a CriteriaQuery for the Product entity
CriteriaQuery<Product> criteriaQuery = builder.createQuery(Product.class);

// Specify the root entity (in this case, Product)
Root<Product> productRoot = criteriaQuery.from(Product.class);

// Define the conditions for our query
criteriaQuery.select(productRoot)
    .where(
        builder.isTrue(productRoot.get("inStock")),  // Products that are currently in stock
        builder.lessThan(productRoot.get("price"), 50) // Products with a price less than $50
    );

// Execute the query and get the result
List<Product> affordableProductsInStock = entityManager.createQuery(criteriaQuery).getResultList();

In this example, we're using the Criteria API to construct a query for products that are currently in stock and have a price lower than $50. It's like telling our database, "Hey, can you find me all the products that are available and cost less than $50?" And the Criteria API takes care of translating that request into a database query, giving us the list of affordable products in stock.

Waytojava is designed to make learning easier. We simplify examples for better understanding. We regularly check tutorials, references, and examples to correct errors, but it's important to remember that humans can make mistakes.