read and write blobs by mapping them to binary data

心不动则不痛 提交于 2019-12-25 01:35:20

问题


I'm getting the following exception trying to read a blob from a Sybase DB using hibernate JPA.

Entity

@Lob
@Column(length=100000)    
private byte[] fileContent;

public byte[] getFileContent() {
    return fileContent;
}

public void setFileContent(byte[] fileContent) {
    this.fileContent = fileContent;
}

ioc.Registry The method com.sybase.jdbc3.jdbc.SybResultSet.getBlob(String) is not supported and should not be called. ioc.Registry Operations trace: ioc.Registry [ 1] Triggering event 'activate' on Purchase_Request TapestryModule.RequestExceptionHandler Processing of request failed with uncaught exception: The method com.sybase.jdbc3.jdbc.SybResultSet.getBlob(String) is not supported and should not be called. org.apache.tapestry5.ioc.internal.OperationException: The method com.sybase.jdbc3.jdbc.SybResultSet.getBlob(String) is not supported and should not be called.

I come across the following hibernate thread which provided a link to an example of how to read and write blobs by mapping them to binary data, however the link is dead.

Thread https://forum.hibernate.org/viewtopic.php?f=1&t=936553

Dead Link http://www.hibernate.org/73.html

I'm wondering if anybody could provide an example or an article describing how to do this?

UPDATE

I found the following JIRA issue outlining this problem https://issues.jboss.org/browse/JBPAPP-2867

Laura claims the "The workaround for this issue is to create user-defined types that map to the Sybase text and image types."

Is anybody familiar with creating a user defined type?


回答1:


You can try using java.sql.Blob instead of byte array in your mapping. You can use Hibernate.createBlob() function to convert a blob from byte array, String, input stream. The advantage of this is lazy loading ( Only till the hibernate session is open).

Otherwise..in memory loading of larger object can consume lot of heap space.

 private void setBlob(Blob blob) 
 {
    this.image = toByteArray(blob);
 }

 private Blob getBlob()
 {
   return Hibernate.createBlob(this.image);
 }


来源:https://stackoverflow.com/questions/10141373/read-and-write-blobs-by-mapping-them-to-binary-data

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