hibernate blob 对象序列化保存

阅读更多

hibernate blob 对象序列化保存

 

 

本文 是 在数据库中存储一个 estatic search 对象 ESQueryCondition  该对象 。

 

代码走起,

 

bean

package com.cmcc.aoi.selfhelp.entity;

import java.io.Serializable;
import java.sql.Blob;
import java.util.Date;

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

import org.slave4j.orm.hibernate.BaseEntity;

/**
 * @author baoyou  E-mail:[email protected]
 * @version 创建时间:2015年11月2日 下午2:27:10 
 * des:
 */
@Entity
@Table(name = "estask")
public class EsTask extends BaseEntity implements Serializable {
	@Id
	@Column(name = "es_id")
	private String esId;
	
	@Lob()
	@Column(name = "es_qcserializable")
	private Blob esQcserializable;
	 
	@Column(name = "es_qcjsonstring")
	private String esQcjsonstring;
	
	@Column(name = "es_send_status")
	private int esSendStatus;
	
	@Column(name = "request_id")
	private String requestId;
	
	@Column(name = "insert_time")
	private Date insertTime;

	@Column(name = "last_update_time")
	private Date lastUpdateTime;
	
	
	
	public String getEsId() {
		return esId;
	} 
	public void setEsId(String esId) {
		this.esId = esId;
	} 
	 
	public Blob getEsQcserializable() {
		return esQcserializable;
	}
	public void setEsQcserializable(Blob esQcserializable) {
		this.esQcserializable = esQcserializable;
	}
	public String getEsQcjsonstring() {
		return esQcjsonstring;
	} 
	public void setEsQcjsonstring(String esQcjsonstring) {
		this.esQcjsonstring = esQcjsonstring;
	} 
	public int getEsSendStatus() {
		return esSendStatus;
	} 
	public void setEsSendStatus(int esSendStatus) {
		this.esSendStatus = esSendStatus;
	} 
	public String getRequestId() {
		return requestId;
	} 
	public void setRequestId(String requestId) {
		this.requestId = requestId;
	}
	public Date getInsertTime() {
		return insertTime;
	}
	public void setInsertTime(Date insertTime) {
		this.insertTime = insertTime;
	}
	public Date getLastUpdateTime() {
		return lastUpdateTime;
	}
	public void setLastUpdateTime(Date lastUpdateTime) {
		this.lastUpdateTime = lastUpdateTime;
	} 
}

 

EsTask et = new EsTask();
et.setEsId(deliverWebRequest.getRequestId()+"");
et.setEsQcjsonstring(wstf.getWkjsTag());
et.setEsQcserializable(HibernateBlobUtil.ObjectToBlob(getESQueryCondition(wstf.getWkjsTag() ,req)));
et.setEsSendStatus(-1);
et.setLastUpdateTime(new Date());
et.setInsertTime(new Date());
esTaskService.save(et);

 取

ESQueryCondition query =  (ESQueryCondition) HibernateBlobUtil.BlobToObject( et.getEsQcserializable());

 

工具类

package com.cmcc.aoi.util;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

import org.hibernate.Hibernate;

/**
 * @author baoyou E-mail:[email protected]
 * @version 创建时间:2015年11月3日 下午6:47:56 des:
 */
public class HibernateBlobUtil {

	public static java.sql.Blob ObjectToBlob(Object obj)  {
		try {
			ByteArrayOutputStream out = new ByteArrayOutputStream();
			ObjectOutputStream outputStream = new ObjectOutputStream(out);
			outputStream.writeObject(obj);
			byte[] bytes = out.toByteArray();
			outputStream.close();
			return Hibernate.createBlob(bytes);
		} catch (Exception e) {
			return null;
		}

	}

	public static Object BlobToObject(java.sql.Blob desblob)   {
		try {
			Object obj = null;
			ObjectInputStream in = new ObjectInputStream(
					desblob.getBinaryStream());
			obj = in.readObject();
			in.close();
			return obj;
		} catch (Exception e) { 
			e.printStackTrace();
		}
		return null;
	}
	
	
}

 

测试结果 ,一切ok .....

 

  

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

捐助开发者

在兴趣的驱动下,写一个免费的东西,有欣喜,也还有汗水,希望你喜欢我的作品,同时也能支持一下。 当然,有钱捧个钱场(右上角的爱心标志,支持支付宝和PayPal捐助),没钱捧个人场,谢谢各位。


hibernate blob 对象序列化保存_第1张图片hibernate blob 对象序列化保存_第2张图片hibernate blob 对象序列化保存_第3张图片
 
 
 谢谢您的赞助,我会做的更好!

 

 

你可能感兴趣的:(hibernate,blob,对象序列化保存)