Steve Daskam's Blog

Blog about the daily practices and methods of an agile software developer.

Using JPA to Insert and Retrieve BLOBs and CLOBs

leave a comment »

I spent some time recently exploring how to insert and retrieve BLOBs and CLOBs in Oracle using JPA/Hibernate. Here’s an example of how to do it:

The table:

CREATE table large_table_demo (
table_id Integer,

clob_column CLOB,

blob_column BLOB

);

The JPA annotated class:

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Lob;
import javax.persistence.Table;

@Entity
@Table(name="LARGE_TABLE_DEMO")
public class LargeTable {
	private Long tableId;
	private String clob;
	private MyObject blob;
	
	@Id
	@Column(name="TABLE_ID")
	public Long getTableId() {
		return tableId;
	}
	
	public void setTableId(Long tableId) {
		this.tableId = tableId;
	}
	
	@Lob
	@Column(name="CLOB_COLUMN")
	public String getClob() {
		return clob;
	}
	
	public void setClob(String clob) {
		this.clob = clob;
	}

	@Lob
	@Column(name="BLOB_COLUMN")
	public MyObject getBlob() {
		return blob;
	}

	public void setBlob(MyObject blob) {
		this.blob = blob;
	}
	
}

The Test Class:

public class BlobTest extends BaseDaoTests {

    @Autowired
    protected BlobDao blobDao;
	
    public void testJpaBlobSave() throws Exception {
        // Read in serialized object
        FileInputStream f_in = new FileInputStream("c:\\myobject.data");
        ObjectInputStream obj_in = new ObjectInputStream (f_in);
        Object obj = obj_in.readObject();
        MyObject myObject = (MyObject)obj;
	    	
        LargeTable lt = new LargeTable();
        lt.setTableId(1L);
        lt.setBlob(myObject);
	    	
        // Save file to DB
        blobDao.saveObject(lt);    	
    }
    
    public void testJpaBlobRetrieve() throws Exception {
    	Object obj = blobDao.find(LargeTable.class, 1L);
    	LargeTable lt = (LargeTable)obj;
        assertEquals(Long.valueOf(1L), lt.getTableId());
    }

    public void testClobSave() throws Exception {
       // Read in text file
       String s = readFileAsString("c:\\clobtest.txt");
       LargeTable lt = new LargeTable();
       lt.setTableId(2L);
       lt.setClob(s);
    		
       blobDao.saveObject(lt);
    }

    public void testClobRetrieve() throws Exception {
        Object obj = blobDao.find(LargeTable.class, 2L);
        LargeTable lt = (LargeTable)obj;
        assertEquals(Long.valueOf(2L), lt.getTableId());
    }
	
    private String readFileAsString(String filePath) throws java.io.IOException {
        StringBuffer fileData = new StringBuffer();
        BufferedReader reader = new BufferedReader(
                new FileReader(filePath));
        char[] buf = new char[1024];
        int numRead=0;
        while((numRead=reader.read(buf)) != -1){
            String readData = String.valueOf(buf, 0, numRead);
            fileData.append(readData);
            buf = new char[1024];
        }
        reader.close();
        return fileData.toString();
    }
}

Advertisement

Written by stevedaskam

March 28, 2011 at 9:57 pm

Posted in Hibernate, JPA

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com 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

Follow

Get every new post delivered to your Inbox.