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
