java hibernate 映射和注解oracle含有blob字段的数据表的pojo源码

3 篇文章 0 订阅

       将oracle数据表blob字段映射到hibernate pojo的byte[]属性,构造pojo时,直接传入byte数组即可,如果需要传入一个File对象,只需使用FileUtils.readFileToByteArray(file)将File转换为byte[]型字节流数组即可, 闲话不说,直接贴源码:

package db.hbm;

import java.sql.Blob;

import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Lob;
import javax.persistence.Table;

import org.hibernate.annotations.GenericGenerator;
import org.hibernate.annotations.Type;
import org.hibernate.annotations.TypeDef;
import org.hibernate.annotations.TypeDefs;
import org.springframework.orm.hibernate3.support.BlobByteArrayType;


/**
 * Files entity. @author MyEclipse Persistence Tools
 */
@TypeDefs({@TypeDef(name="pic",typeClass=BlobByteArrayType.class)})
@Entity
@Table(name = "FILE")
public class File implements java.io.Serializable {


	// Fields
	private String id;
	private String filename;
	private byte[] data; //使用byte[]数组映射blob字段
	
	// Constructors
	/** default constructor */
	public File() {
	}

	/** full constructor */
	public File(String filename,byte[] data) {
		this.filename = filename;
		this.data = data;
	}

	// Property accessors
	@GenericGenerator(name = "generator", strategy = "uuid")
	@Id
	@GeneratedValue(generator = "generator")
	@Column(name = "ID", unique = true, nullable = false, length = 50)
	public String getId() {
		return this.id;
	}

	public void setId(String id) {
		this.id = id;
	}

	@Column(name = "FILENAME", nullable = false, length = 200)
	public String getFilename() {
		return this.filename;
	}


	public void setFilename(String filename) {
		this.filename = filename;
	}
	
	@Lob
	@Basic(fetch=FetchType.LAZY)
	@Type(type = "org.springframework.orm.hibernate3.support.BlobByteArrayType")
        @Column(columnDefinition = "BLOB", name = "DATA", nullable = false)   

	//@Type(type="pic")
        //@Column(name = "DATA", nullable = false)   
	public byte[] getData() {
		return this.data;
	}

	public void setData(byte[] data) {
		this.data = data;
	}


}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值