From the course: Java Persistence with JPA and Hibernate

Unlock the full course today

Join today to access over 24,400 courses taught by industry experts.

Named queries

Named queries

- [Instructor] Named queries are no different than SQL and JPQL queries. It's just that you are grouping your queries in a single place and later referring to them using predefined names whenever you need to use them. Usually the place you would define your named queries would be your entity classes. The benefit of this is reusability and clean code because queries are no longer scattered in multiple places in your code. A named query is a statically-defined query with a predefined unchangeable query string defined using the @NamedQuery annotation. Let's start by running mariadb-init SQL script, followed by mariadb-07_03 SQL Script. Let's create a named query in the book type entity class. Let's write a simple named query to find all the book types. There are three records in the book_type table. At the top of the BookType entity class, you can place your named query. Write the @NamedQuery annotation, @NamedQuery. It has a name attribute to which you can give a name for your query…

Contents