In my previous article I have explained about how to use @Query annotation for writing the custom queries for Spring Data JPA applications. In this post, I will explain another type of writing the custom queries using @NamedQuery
annotation that is part of the JPA specification.
What is NamedQuery?
- There are two types of queries in JPA. One is dynamic and another one is static. Named query fall under the second category.
- Named queries are a way of organizing your static queries in a manner that is more readable, maintainable.
- The named queries are defined in the single place at entity class itself with each query has its unique name. That is the reason it is known as named query.
- @NamedQuery annotation can be applied only at the class level.
- Note that named queries have the global scope (can be accessed throughout the persistence unit), so every query should be uniquely identified even if you define queries for different entities.
- Let’s say, you have a named query with name findOne for an entity Employee, but if you are trying to write another named query with same name fineOne for another entity User, then you will encounter duplicate named query exception. That is the main reason behind prefix the named query name with entity name so that it is uniquely referred.
There are two annotation used for defining the named queries,
- If you are defining single query, then use
@NamedQuery
- If you have to define more than one named queries, then use
@NamedQueries
How to use @NamedQuery?
The following are the steps required to implement named queries in your application.
Define @NamedQuery in Entity Class
First, create named queries in your entity class. All the named queries must be written only in the entity classes, query methods will not have any queries defined at method level.
The example definition would look like this:
@Entity @NamedQuery(name="Book.findByPrice", query="select name,author,price from Book b where b.price=?1") public class Book implements Serializable{ private static final long serialVersionUID = 1L; @Id long id; @Column(name="name") String name; @Column(name="author") String author; @Column(name="price") long price; //getters and setters
Create Repository with Query Method
Second, Create Spring Data Custom Repository and create a query method. The method name and the named query name in entity class must be same. Ensure that you are not using @Query
annotation with custom query. Because, @Query
always takes precedence than other options.
Here is the sample repository interface for the above named query.
public interface BookNamedQueryRepositoryExample extends Repository<Book, Long> { // Query will be used from Named query defined at Entity class List<Book> findByPrice(long price); }
When to use NamedQuery?
Named queries are ideal when you have the query that is very less likely would change at run time. Here I would list down the characteristics of the named query, it will help you to arrive at your decision whether you have to go for named query or not.
- All the named queries are validated at application start-up time and there is no failure at run time.
- It is easier to maintain since all the queries are stored in single location.
- HQL and native SQL queries can be used and replaced without code changes (no need to re-compile your code).
- There is no support for the dynamic queries.
- We can write the complicated SQL queries and map the results to objects.
- When we are moving existing application to use Spring Data JPA, then we are not required to move the named queries to methods. You have to just create query methods for each named query and map it.
Source Code Download
This example source code contains many examples of Spring Data JPA and not just for @NamedQuery
. This example uses Spring Boot for packaging and deploying spring application.
Conclusion
In this tutorial I have explained how to use @NamedQuery
and the advantages of using named queries. Using named queries are ideal when the queries are static and less likely modified at the tun time. Also it is easy to migrate an existing applications to spring data jpa.
If you have used named queries in your project, please share your experience and thoughts in the comments section. The best comments will be updated to this tutorial and will be select for free book promotions.