Create Blob in Hibernate

Blob field can be populated using  Hibernate.createBlob().

Map your persistent column to java.sql.Blob type.

    import java.sql.Blob;
    @Table( name = "BLOB_TEST" )
    public class BlobTable {
         // Id column goes here.
          @Column( name = "blob_col" )
         private Blob blobCol;
         // Getter and setter codes here

Create blog using file.

 FileInputStream fis = new FileInputStream( "D:/export.txt" );
 BlobTable table = new BlobTable();
 table.setBlobCol( Hibernate.createBlob( fis ) );

PS: you can also use Hibernate.createBlob(byte[])

Persist the entity (BlobTable) using<ENTITY_OBJ>) (or) entityManager.persist(<ENTITY_OBJ>). Once persisted, verify the size of the blob column using the below SQL.

select dbms_lob.getlength(<BLOB_COL>) from <BLOB_TABLE>;

It should match the file size in bytes.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: