JPA supports five kinds of queries: JPQL queries, Criteria API queries, native SQL queries, named queries, and stored procedure queries. JPQL and the Criteria API work on the entity model and are portable across databases. Native SQL gives you full database features, and named queries are JPQL or SQL declared once and referenced by name.
Since Jakarta EE 9 the API lives in the jakarta.persistence package (older code uses javax.persistence). Every example below uses jakarta.persistence and works the same way with Hibernate 6 and 7, EclipseLink, and other providers.
| Type | Defined with | Checked | Best for |
|---|---|---|---|
| JPQL | String passed to createQuery | At run time (and by IDE tooling) | Most static queries |
| Criteria API | Java builder objects | Partly at compile time, fully with the static metamodel | Dynamic filters built at run time |
| Native SQL | Plain SQL string | By the database | Vendor features, tuned or legacy SQL |
| Named query | @NamedQuery / @NamedNativeQuery | Often validated at startup | Reused, stable queries |
| Stored procedure | StoredProcedureQuery | By the database | Calling existing database procedures |
All of them end up as a Query or TypedQuery object, so executing and paging works the same way for each.
JPQL (Jakarta Persistence Query Language) looks like SQL but uses entity names and attribute names instead of tables and columns. The provider translates it to SQL for your database dialect.
Always bind values with named (:name) or positional (?1) parameters. Never concatenate user input into the string, because that opens the door to injection and defeats statement caching.
JPQL also supports joins, JOIN FETCH to load associations in one query, aggregates, GROUP BY, subqueries, constructor expressions and bulk UPDATE and DELETE statements:
Constructor expressions map straight to a DTO or record, which avoids loading whole entities when you only need a few columns.
The Criteria API builds the same kind of query from Java objects. It is useful when the set of conditions changes at run time, for example a search form where every field is optional.
The string "name" in user.get("name") is not checked by the compiler. To get compile-time safety, generate the static metamodel (the User_ class, created by the Hibernate or EclipseLink annotation processor) and write user.get(User_.name) instead.
A native query sends SQL to the database as written. Use it for window functions, recursive CTEs, database-specific hints and other features JPQL does not cover.
When you pass an entity class, the result columns must match the entity mapping. For custom shapes, use @SqlResultSetMapping or let your provider map to a DTO. The trade-off is portability: a native query is tied to one database, and the persistence context is not told which tables it touched, so flush before running one that depends on pending changes.
A named query is defined once, usually on the entity, and referenced by name. Providers can parse and validate it when the persistence unit starts, so mistakes surface early instead of on the first call.
@NamedNativeQuery does the same for SQL. Spring Data JPA can bind a repository method to a named query by the Entity.method naming convention, or you can use @Query on the repository method.
JPA 2.1 added StoredProcedureQuery for calling procedures that already exist in the database:
You can also declare it with @NamedStoredProcedureQuery. Like native SQL, this ties the code to the database.
Every query type shares the same execution methods:
getResultList() returns a List, and getResultStream() returns a Stream that you must close.getSingleResult() expects exactly one row and throws NoResultException or NonUniqueResultException otherwise.executeUpdate() runs bulk UPDATE and DELETE statements.setFirstResult(int) and setMaxResults(int) implement paging.Always add an ORDER BY when you page, otherwise the page content is not deterministic. Bulk updates bypass the persistence context, so entities already loaded can be stale until you call refresh or clear.
Use JPQL for fixed queries because it is shorter and easier to read. Use the Criteria API when the conditions are assembled at run time, and add the static metamodel to keep it type-safe.
A dynamic query is built from a string or a criteria object at the call site, while a named query is declared once and looked up by name. Named queries are validated and parsed earlier and keep query text out of business code.
Use one when you need a database feature JPQL lacks, such as window functions or a hand-tuned statement. Accept that it will not be portable across databases.
No. Derived repository methods, @Query annotations and specifications are generated or executed as JPQL, Criteria or native queries underneath.
JPA offers JPQL, Criteria API, native SQL, named queries and stored procedure queries. Prefer JPQL and named queries for static cases, the Criteria API for run-time filters, and native SQL only when you need database-specific power. Bind parameters, order paged results and remember that bulk statements skip the persistence context.