calling jdbc

This commit is contained in:
Gavin 2023-05-13 11:34:08 +02:00 committed by Christian Beikov
parent 8c2e83748a
commit 6d398db1fa
1 changed files with 27 additions and 1 deletions

View File

@ -594,6 +594,7 @@ As we said <<introduction,right up front>>, Hibernate's generated SQL is meant t
| Selection query | `createNativeQuery(String,Class)` | `createNativeQuery(String,Class)` | `getResultList()`, `getSingleResult()`, or `getSingleResultOrNull()`
| Mutation query | `createNativeMutationQuery(String)` | `createNativeQuery(String)` | `executeUpdate()`
| Stored procedure | `createStoredProcedureCall(String)` | `createStoredProcedureQuery(String)` | `execute()`
|===
For the most simple cases, Hibernate can infer the shape of the result set:
@ -687,4 +688,29 @@ List<Book> books =
[[named-queries]]
=== Named queries
TODO
TODO
[[jdbc]]
=== Interacting directly with JDBC
From time to time we run into the need to write some code that calls JDBC directly.
Unfortunately, JPA offers no good way to do this, but the Hibernate `Session` does.
[source,java]
----
session.doWork(connection -> {
try (var callable = connection.prepareCall("{call myproc(?)}")) {
callable.setLong(1, argument);
callable.execute();
}
});
----
The `Connection` passed to the work is the same connection being used by the session, and so any work performed using that connection occurs in the same transaction context.
[TIP]
====
You can call stored procedures using `createStoredProcedureQuery()` or `createStoredProcedureCall()`.
====
If the work returns a value, use `doReturningWork()` instead of `doWork()`.