liveBook · Manning

JDBC LOB classes

As explained in Java Persistence with Spring Data and Hibernate epub

JDBC LOB classes represent database character or binary large objects and provide access to their contents on demand.

Use java.sql.Blob for large binary values such as images and java.sql.Clob for large character values. Unlike @Lob fields declared as byte[] or String, these JDBC properties can load on demand without bytecode instrumentation. section 6.3.1

Map and read an image

Declare an entity field as @Lob java.sql.Blob imageBlob. After loading the entity, call item.getImageBlob().getBinaryStream() within the same transaction to read its contents. section 6.3.1

The property initially holds a placeholder; accessing it materializes or streams the value. Streaming can avoid temporary buffering. The tradeoff is a domain model coupled to JDBC: accessing LOB properties in unit tests requires a database connection. section 6.3.1

Session session = em.unwrap(Session.class);           #1
Blob blob = session.getLobHelper()                    #2
        .createBlob(imageInputStream, byteLength);
someItem.setImageBlob(blob);
em.persist(someItem);

Hibernate’s LOB helper creates a Blob from an InputStream and its byte length before the entity is persisted. section 6.3.1

How Java Persistence with Spring Data and Hibernate epub covers it

How deep it goes
Explains placeholder values, transaction-bound access, streaming, and Blob creation; defers bytecode-instrumentation details.
Examples
Shows an entity mapping, binary-stream reading, and Blob creation from an InputStream with Hibernate’s LOB helper.
What it assumes
Assumes familiarity with Java entity properties and JPA persistence while demonstrating JDBC LOB declarations and stream access.
What you can do after
Apply it via Hibernate’s LOB helper to create Blobs and access JDBC LOB properties within a transaction.
How it connects
Contrasts JDBC Blob and Clob with @Lob byte[] and String mappings, linking lazy loading to bytecode instrumentation and transactions.

This explainer was generated by Manning's knowledge graph from the text of Java Persistence with Spring Data and Hibernate epub. The section links point to the parts of the book it draws on.