JPA Buddy
announcement - icon

JPA is huge! It covers nearly every aspect of communication between relational databases and the Java application and is deeply integrated into all major frameworks.

If you're using IntelliJ, JPA Buddy is super helpful. The plugin gently guides you through the subtleties of the most popular JPA implementations, visually reminds you of JPA features, generates code that follows best practices, and integrates intelligent inspections to improve your existing persistence code.

More concretely, it provides powerful tooling to generate Spring Data JPA repositories and methods, Flyway Versioned Migrations, Liquibase Differential Changelogs, DDL and SQL statements, DTO objects, and MapStruct interfaces.

Oh, and it actually generates JPA entities from an existing database and gradually update the data model as the database evolves! Yeah.

>> Become a lot more productive with JPA Buddy

Persistence top

Get started with Spring Data JPA through the reference Learn Spring Data JPA course:


1. Overview

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 image, audio, or video

In this tutorial, we'll show how we can utilize Hibernate ORM for persisting large objects.

2. Setup

For example, we'll use Hibernate 5 and H2 Database. Therefore we must declare them as dependencies in our pom.xml:


The latest version of the dependencies is in Maven Central Repositories.

For a more in-depth look at configuring Hibernate please refer to one of our introductory articles.

3. LOB Data Model

Our model “User” has id, name, and photo as properties. We'll store an image in the User‘s photo property, and we will map it to a BLOB:

public class User {

    private String id;
    @Column(name = "name", columnDefinition="VARCHAR(128)")
    private String name;
    @Column(name = "photo", columnDefinition="BLOB")
    private byte[] photo;

    // ...

The @Lob annotation specifies that the database should store the property as Large Object. The columnDefinition in the @Column annotation defines the column type for the property.

Since we're going to save byte array, we're using BLOB.

4. Usage

4.1. Initiate Hibernate Session

session = HibernateSessionUtil

Using the helper class, we will build the Hibernate Session using the database information provided in file.

4.2. Creating User Instance

Let's assume the user uploads the photo as an image file:

User user = new User();
InputStream inputStream = this.getClass()

if(inputStream == null) {
    fail("Unable to get resources");

We convert the image file into the byte array by using the help of Apache Commons IO library, and finally, we assign the byte array as part of the newly created User object.

4.3. Persisting Large Object

By storing the User using the Session, the Hibernate will convert the object into the database record:


Because of the @Lob annotation declared on the class User, Hibernate understands it should store the “photo” property as BLOB data type.

4.4. Data Validation

We'll retrieve the data back from the database and using Hibernate to map it back to Java object to compare it with the inserted data.

Since we know the inserted Users id, we will use it to retrieve the data from the database:

User result = session.find(User.class, "1");

Let's compare the query's result with the input User‘s data:

  "Query result is null", 
  "User's name is invalid", 
  user.getName(), result.getName() );
  "User's photo is corrupted", 
  Arrays.equals(user.getPhoto(), result.getPhoto()) );

Hibernate will map the data in the database to the Java object using the same mapping information on the annotations.

Therefore the retrieved User object will have the same information as the inserted data.

5. Conclusion

LOB is datatype for storing large object data. There're two varieties of LOB which is called BLOB and CLOB. BLOB is for storing binary data, while CLOB is for storing text data.

Using Hibernate, we have demonstrated how it's quite easy to map the data to and from Java objects, as long as we're defining the correct data model and the appropriate table structure in the database.

As always the code for this article is available over on GitHub.

Persistence bottom
Get started with Spring Data JPA through the reference Learn Spring Data JPA course: >> CHECK OUT THE COURSE
Persistence footer banner
Comments are closed on this article!