14.1. Creating and running queries

Let's start with a few examples so you understand the basic usage. In earlier chapters, we mentioned that there are three ways to express queries in Hibernate:

  • Hibernate Query Language (HQL), and the subset standardized as JPA QL:

    session.createQuery("from Category c where c.name like 'Laptop%'");
    entityManager.createQuery(
        "select c from Category c where c.name like 'Laptop%'"
    );
    
  • Criteria API for query by criteria (QBC) and query by example (QBE):

    session.createCriteria(Category.class)
            .add( Restrictions.like("name", "Laptop%") );
    
  • Direct SQL with or without automatic mapping of resultsets to objects:

     session.createSQLQuery( "select {c.*} from CATEGORY {c} where NAME like 'Laptop%'" ).addEntity("c", Category.class); ...

Get Java Persistence with Hibernate now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.