How do I use the BLOB data type with Hibernate?

北城余情 提交于 2019-12-05 07:44:45
RBz

Finally got the answer, seems pretty simple now.

I mapped oracle blob to byte array, changed my entity class to

@Lob
@Column(name = "DTA_BLOB")
private byte[] DataBlob;

/**
* @return the DataBlob
*/
public byte[] getDataBlob(){
    return DataBlob;
}

/**
* @param DataBlob the DataBlob to set
*/
public void setDataBlob(byte[] DataBlob) {
    this.DataBlob = DataBlob;
}

And the DAO will be like:

@Override
@Transactional
public byte[] getMenu(Long menuDataId) throws SQLException, IOException
{
    MenuData menu_data = this.entityManager.find(MenuData.class,menuDataId);  
    return menu_data.getDataBlob();
}

Oracle blob can be converted to String as shown below.

byte[] bdata = blob.getBytes(1, (int)blob.length()); 
String dataStr = new String(bdata);

Some drivers support getString() on blobs although its risky to use because of encoding. In short, blob code is never that pretty. Keep in mind, string encoding can get ugly for reading blob data.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!