HomeHibernateSave image - working with large objects (@LOB) in JPA with Hibernate

Save image – working with large objects (@LOB) in JPA with Hibernate

In this article you will see how to save image in database and how to work with LOB (Large objects), CLOB (Character large objects) database types to save in database.

1. LOB – large objects in database

  • Databases will provide data types to store large objects (LOBs) in columns, this types will allow gigabyte ranges.
  • LOBs come in two flavors in the database: character large objects, called CLOBs, and binary large objects, or BLOBs.
  • As their names imply, a CLOB column holds a large character sequence, and a BLOB column can store a large byte sequence.
  • Mostly CLOB is to store text based files and BLOB is to store media files like images, videos,mp3 etc..
  • Data type names may vary in different databases(Oracle, MySql etc..).
  • JPA provides @Lob annotation to work with Large objects.

2. Example Application

Technologies Used in all of examples :

  • JPA 2.1
  • Hibernate 5.2.6
  • MySql 8.0
  • Maven 3
  • Spring Tool Suite (STS) 3.9.8
  • Java 1.8
Jpa hibernate save image Project Structure

Database table Structure :

jpa hibernate save image user table structure

User Entity :

public class User {
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "ID")
    private Long id;
    private String userName;
    private String password;
    private Date creationTime;
    private Date updatedTime;
    private Date dateofBirth;
    private UserType userType;
    private byte[] profilePic;
    private transient String dateOfBirthString;

Test Code :

User user = new User();
// Setting Current Date
user.setCreationTime(new Date());
user.setDateofBirth(new Date());
//On windows new File("img\\JBDFav300.png")
File file = new File("img/JBDFav300.png");
byte[] picInBytes = new byte[(int) file.length()];
FileInputStream fileInputStream = new FileInputStream(file);

3. Verify in database

  • Open table data in mysql workbench –> Right click on PROFILE_PIC column –> Select “Open Value In Editor” –> Select “Image” from opened window. you can see saved image in PROFILE_PIC column. See below screen.
save image Large objects LOB in mysql using jpa hibernate

Download Application – JPA-Image.zip (25 KB)

4. Conclusion

In this article we have covered how to save image in database with JPA and Hibernate and How to work with LOB (Large objects) in JPA. In real applications I have never seen use cases such as saving very large (gigabytes files) in DB, generally large files stores in Image servers or cloud or server local drive and path locations and file info stores in DB. If any use case you came across such as saving huge huge gigabytes files in DB please comment in this post.

You also might be interested in following articles :

  1. Ignore fields to save in DB using @Transient annotation in JPA and Hibernate
  2. @Temporal annotation examples in JPA with Hibernate
  3. Hibernate Entity types and Value types.

5. References

  1. JPA document


  1. and what if you save huge files, i mean huge, huge files, like 2GB? it has to be loaded into your memory. your solution is not a solution.

    • Hi Miki, yes that’s true, this is not solution for saving huge files, but this article is not about solution for saving files in real world large applications. This article explains about the concept in JPA how to save large files (LOB/BLOB types) in Database.


Please enter your comment!
Please enter your name here