@Lob - JPA Annotation with Example

The @Lob annotation is used to specify that the currently annotated entity attribute represents a large object type.

LOB or Large OBject refers to a variable-length datatype for storing large objects.

The datatype has two variants:

  • CLOB – Character Large Object will store large text data
  • BLOB – Binary Large Object is for storing binary data like images, audio, or video

In this tutorial, we'll demonstrate the usage of JPA @Lob annotation with an example.

JPA @Lob annotation example - BLOB mapped to byte[]

In this example, we will store the image into a database table (large object) and we will map the BLOB in a materialized form (e.g. byte[]).

Let’s create a DatabaseFile JPA entity to model the file attribute that will be stored in the database:
package net.javaguides.springboot.fileuploaddownload.model;

import org.hibernate.annotations.GenericGenerator;

import javax.persistence.*;

@Entity
@Table(name = "files")
public class DatabaseFile {
    @Id
    @GeneratedValue(generator = "uuid")
    @GenericGenerator(name = "uuid", strategy = "uuid2")
    private String id;

    private String fileName;

    private String fileType;

    @Lob
    private byte[] data;

    public DatabaseFile() {

    }

    public DatabaseFile(String fileName, String fileType, byte[] data) {
        this.fileName = fileName;
        this.fileType = fileType;
        this.data = data;
    }

    public String getId() {
        return id;
    }

    public String getFileName() {
        return fileName;
    }

    public String getFileType() {
        return fileType;
    }

    public byte[] getData() {
        return data;
    }

    public void setId(String id) {
        this.id = id;
    }

    public void setFileName(String fileName) {
        this.fileName = fileName;
    }

    public void setFileType(String fileType) {
        this.fileType = fileType;
    }

    public void setData(byte[] data) {
        this.data = data;
    }
}
The @Lob annotation specifies that the database should store the property as a Large Object:
    @Lob
    private byte[] data;
Note that, the file’s contents will be stored as a byte array in the database.

JPA @Lob annotation example - BLOB mapped to java.sql.Blob type

In this example, we will map BLOB to JDBC java.sql.Blob type in JPA entity.

Considering we have the following database table:

BLOB - SQL database table:

CREATE TABLE Product (
    id INTEGER NOT NULL ,
    image blob ,
    name VARCHAR(255) ,
    PRIMARY KEY ( id )
)

Let’s first map this using the JDBC java.sql.Blob type in JPA entity:

BLOB mapped to java.sql.Blob:

@Entity(name = "Product")
public static class Product {

    @Id
    private Integer id;

    private String name;

    @Lob
    private Blob image;

    //Getters and setters are omitted for brevity

}

To persist such an entity, you have to create a Blob using the BlobProxy Hibernate utility:

Persisting a java.sql.Blob entity:

byte[] image = new byte[] {1, 2, 3};

final Product product = new Product();
product.setId( 1 );
product.setName( "Mobile phone" );

product.setImage( BlobProxy.generateProxy( image ) );

entityManager.persist( product );

To retrieve the Blob content, you need to transform the underlying java.io.InputStream:

Returning a java.sql.Blob entity:

Product product = entityManager.find( Product.class, productId );

try (InputStream inputStream = product.getImage().getBinaryStream()) {
    assertArrayEquals(new byte[] {1, 2, 3}, toBytes( inputStream ) );
}

References

Comments