1.hibernate3
public class BlobTest{ private byte[] portrait; @Column(name = "PORTRAIT", nullable = true, length = 0) public byte[] getPortrait() { return portrait; }}Object v = map.get(n);v:是 从map中获取,map为返回的键值集合。f:类中定义的属性,就是portrait ,if(f.getType().getName().equals(byteTypeName.getClass().getName())){ try { byte[] imageData=null; SerializableBlobProxy blob = (SerializableBlobProxy)Proxy.getInvocationHandler(v); BufferedInputStream stream = new BufferedInputStream(blob.getWrappedBlob().getBinaryStream()); ByteArrayOutputStream out = new ByteArrayOutputStream(); byte[] buff = new byte[1024]; int i = 0; while((i=stream.read(buff))!=-1){ imageData=ArrayUtils.addAll(imageData, buff); } BeanUtils.setProperty(obj, n, imageData); }catch (SQLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }
2.hibernate4
类:@Column(name = "PORTRAIT", nullable = true, length = 0) @Basic @Lob public byte[] getPortrait() { return portrait; } v = map.get(str); if (v != null) { byte[] byteTypeName={1}; if(f.getType().getName().equals(byteTypeName.getClass().getName())){ try { byte[] imageData=null; SerializableBlob blob = (SerializableBlob)v; BufferedInputStream stream = new BufferedInputStream(blob.getWrappedBlob().getBinaryStream()); ByteArrayOutputStream out = new ByteArrayOutputStream(); byte[] buff = new byte[1024]; int i = 0; while((i=stream.read(buff))!=-1){ mageData=ArrayUtils.addAll(imageData, buff); } BeanUtils.setProperty(obj, n, imageData); }catch (SQLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }