liveBook · Manning

Binary large object

As explained in Java Persistence with Spring Data and Hibernate epub

A binary large object is binary data stored as a single large database value, commonly for multimedia content.

A binary large object stores a large binary value, such as an image, in a database column. The Java property type determines how Hibernate maps and loads that value. section 6.3.1

For an entity’s image property, the main mapping choices are:

  • Use byte[] for the default VARBINARY mapping; the native column type depends on the SQL dialect and declared length. section 6.3.1
  • Add @Lob to byte[] to map it as a BLOB. The bytes still load when the entity loads unless field-access interception is enabled through bytecode instrumentation. section 6.3.1
  • Use java.sql.Blob for lazy loading without bytecode instrumentation; obtain its binary data through a stream. section 6.3.1
Item item = em.find(Item.class, ITEM_ID);
InputStream imageDataStream = item.getImageBlob().getBinaryStream();     #1
ByteArrayOutputStream outStream = new ByteArrayOutputStream();           #2
StreamUtils.copy(imageDataStream, outStream);                            #3
byte[] imageBytes = outStream.toByteArray();

The stream-reading example shows how to obtain image bytes from a Blob property after finding the entity. section 6.3.1

How Java Persistence with Spring Data and Hibernate epub covers it

How deep it goes
Compares three property mappings and their loading behavior; defers bytecode-instrumentation details.
Examples
Shows image properties mapped as byte[] or java.sql.Blob and a stream used to read Blob data.
What you can do after
Evaluate/understand whether byte[], @Lob, or java.sql.Blob fits a large binary property’s loading needs.
How it connects
Connects BLOBs to VARBINARY, JPA @Lob, JDBC large objects, SQL dialects, and lazy loading.

Also explained in

Compare how 2 books explain binary large object →

Related concepts

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.