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

       将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;
	}


}


展开阅读全文

没有更多推荐了,返回首页