From 65a500182fad4c0a2526c8529653fe23611688be Mon Sep 17 00:00:00 2001 From: Gavin Date: Sat, 13 May 2023 19:23:33 +0200 Subject: [PATCH] short section on named queries --- .../asciidoc/introduction/Interacting.adoc | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/documentation/src/main/asciidoc/introduction/Interacting.adoc b/documentation/src/main/asciidoc/introduction/Interacting.adoc index 5043256ed1..3b0e292a37 100644 --- a/documentation/src/main/asciidoc/introduction/Interacting.adoc +++ b/documentation/src/main/asciidoc/introduction/Interacting.adoc @@ -724,6 +724,25 @@ We have to make sure that the class with the `@NamedQuery` annotation will be sc The `@NamedNativeQuery` lets us do the same for native SQL queries. +.Executing named queries +|=== +| Kind of query | `Session` method | `EntityManager` method | `Query` execution method + +| Selection query | `createNamedSelectionQuery(String,Class)` | `createNamedQuery(String,Class)` | `getResultList()`, `getSingleResult()`, or `getSingleResultOrNull()` +| Mutation query | `createNamedMutationQuery(String)` | `createNamedQuery(String)` | `executeUpdate()` +|=== + +We execute our named query as follows: + +[source,java] +---- +em.createNamedQuery("10BooksByTitle") + .setParameter("titlePattern", titlePattern) + .getResultList() +---- + +Note that the code which executes the named query is not aware of whether the query was written in HQL or in native SQL, making it slightly easier to change and optimize the query later. + [[jdbc]] === Interacting directly with JDBC