From 1e22d1bb3d9bc2e94449194b18ccfd252b174bf5 Mon Sep 17 00:00:00 2001 From: Gavin Date: Sat, 13 May 2023 19:14:58 +0200 Subject: [PATCH] short section on named queries --- .../main/asciidoc/introduction/Interacting.adoc | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/documentation/src/main/asciidoc/introduction/Interacting.adoc b/documentation/src/main/asciidoc/introduction/Interacting.adoc index 39b7868e3f..5043256ed1 100644 --- a/documentation/src/main/asciidoc/introduction/Interacting.adoc +++ b/documentation/src/main/asciidoc/introduction/Interacting.adoc @@ -707,7 +707,22 @@ List books = [[named-queries]] === Named queries -TODO +The `@NamedQuery` annotation lets us define a HQL query that is compiled and checked as part of the bootstrap process. +We can place the `@NamedQuery` annotation on any class, even on an entity class. + +[source,java] +---- +@NamedQuery(name="10BooksByTitle", + query="from Book where title like :titlePattern order by title fetch first 10 rows only") +class BookQueries {} +---- + +We have to make sure that the class with the `@NamedQuery` annotation will be scanned by Hibernate, either: + +- by adding `org.hibernate.example.BookQueries` to `persistence.xml`, or +- by calling `configuration.addClass(BookQueries.class)`. + +The `@NamedNativeQuery` lets us do the same for native SQL queries. [[jdbc]] === Interacting directly with JDBC