Home Hibernate Save 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.

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.

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

- Advertisement -

User Entity :

@Entity(name="USER")
public class User {
    
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "ID")
    private Long id;
    
    @Column(name="USER_NAME")
    private String userName;
    
    @Column(name="PASSWORD")
    private String password;
    
    @Temporal(value=TemporalType.TIMESTAMP)
    @Column(name="CREATED_TIME")
    private Date creationTime;
    
    @Temporal(value=TemporalType.TIMESTAMP)
    @Column(name="UPDATED_TIME")
    private Date updatedTime;
    
    @Temporal(value=TemporalType.DATE)
    @Column(name="DOB")
    private Date dateofBirth;
    
    @Enumerated(value=EnumType.STRING)
    @Column(name="USER_TYPE")
    private UserType userType;
    
    @Lob
    @Column(name="PROFILE_PIC")
    private byte[] profilePic;
    
    @Transient
    private transient String dateOfBirthString;

Test Code :

User user = new User();
user.setUserName("mike5");
user.setPassword("password");
// Setting Current Date
user.setCreationTime(new Date());
user.setDateofBirth(new Date());
user.setUserType(UserType.EMPLOYEE);
//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);
fileInputStream.read(picInBytes);
fileInputStream.close();
user.setProfilePic(picInBytes);
    
entityManager.persist(user);
transaction.commit();

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.

saving image in mysql using jpa hibernate

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

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.

References

  1. JPA document
- Advertisement -
Satish Varma
Satish Varmahttps://javabydeveloper.com
Satish is post graduated in master of computer applications and experienced software engineer with focus on Spring, JPA, REST, TDD and web development. Also founder of javabydeveloper.com. Follow him on LinkedIn or Twitter or Facebook

2 COMMENTS

  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.

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Stay in Touch

Categories

Related Articles