3. Bolb类型字段读取:
public ZyglBlxx getBlxx(Map<String,Object> map){
Session session = this.getHibernateTemplate().getSessionFactory().openSession();
ZyglBlxx bean = null;
Criteria criteria = session.createCriteria(ZyglBlxx.class);
Set<String> set = map.keySet();
Iterator<String> it = set.iterator();
while(it.hasNext()){
String key = it.next();
Object value = map.get(key);
criteria.add(Property.forName(key).eq(value));
}
List<ZyglBlxx> list = criteria.list();
if(list.size()>0)bean = list.get(0);
session.close();
return bean;
}
从数据库的读取跟以前一样
字段的读取有点麻烦
public String blobToString(Blob inblob){
String data = null;
if(inblob == null)return data;
SerializableBlob seb = (SerializableBlob)inblob;
try {
BLOB blob = (BLOB)seb.getWrappedBlob();
BufferedInputStream stream = new BufferedInputStream(blob.getBinaryStream());
ByteArrayOutputStream out = new ByteArrayOutputStream();
byte[] buff = new byte[1024];
int i = 0;
while((i=stream.read(buff))!=-1){
out.write(buff,0,i);
}
data = out.toString();
stream.close();
out.close();
} catch (SQLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return data;
}
直接调用inblob.getBytes();得到的是乱码,blob.getBinaryStream().available()得到的是0.对于Hibernate3版本只能用上面的方法来解析。
4. 使用JPA2.0读写Bolb类型字段:
使用JPA2.0读写Blob类型就方便的多了,只需要将Blob类型改为byte[],然后在字段上加上@Basic和@Lob注解就可以了。
更多Oracle相关信息见Oracle 专题页面 http://www.linuxidc.com/topicnews.aspx?tid=12
