From 941fd1487477a1bc9eb744f468567c9513a8b58b Mon Sep 17 00:00:00 2001 From: Gavin Date: Sat, 13 May 2023 20:11:32 +0200 Subject: [PATCH] Blob, Clob --- .../main/asciidoc/introduction/Entities.adoc | 1 + .../main/asciidoc/introduction/Mapping.adoc | 33 +++++++++++++++++++ 2 files changed, 34 insertions(+) diff --git a/documentation/src/main/asciidoc/introduction/Entities.adoc b/documentation/src/main/asciidoc/introduction/Entities.adoc index 40a615e8a5..9dd2c38aab 100644 --- a/documentation/src/main/asciidoc/introduction/Entities.adoc +++ b/documentation/src/main/asciidoc/introduction/Entities.adoc @@ -540,6 +540,7 @@ Hibernate slightly extends this list with the following types: | Classification | Package | Types | Date/time types | `java.time` | `Duration`, `ZoneId`, `ZoneOffset`, `ZonedDateTime`, `Year` +| LOB types | `java.sql` | `Blob`, `Clob` | Miscellaneous | `java.util` | `Currency`, `URL` |==== diff --git a/documentation/src/main/asciidoc/introduction/Mapping.adoc b/documentation/src/main/asciidoc/introduction/Mapping.adoc index a3f205d172..9f0a753629 100644 --- a/documentation/src/main/asciidoc/introduction/Mapping.adoc +++ b/documentation/src/main/asciidoc/introduction/Mapping.adoc @@ -619,3 +619,36 @@ This limitation of the Postgres driver has resulted in a whole cottage industry But simply removing the `@Lob` annotation has exactly the same effect. ==== +Finally, as an alternative, Hibernate lets you declare an attribute of type `java.sql.Blob` or `java.sql.Clob`. + +[source,java] +---- +@Entity +class Book { + ... + Clob text; + Blob coverArt; + .... +} +---- + +To assign a value to these fields, we'll need to use a `LobHelper`. +We can get one from the `Session`: + +[source,java] +---- +LobHelper helper = s.getLobHelper(); +book.text = helper.createClob(text); +book.coverArt = helper.createBlob(image); +---- + +In principle, the `Blob` and `Clob` objects provide efficient ways to read or stream LOB data from the server. + +[source,java] +---- +Book book = s.find(Book.class, bookId); +String text = book.text.getSubString(1, textLength); +InputStream bytes = book.images.getBinaryStream(); +---- + +Of course, the behavior here depends very much on the JDBC driver, and so we really can't promise that this is a sensible thing to do on your database. \ No newline at end of file