`
Janne
  • 浏览: 39355 次
  • 性别: Icon_minigender_2
  • 来自: 成都
社区版块
存档分类
最新评论

hibernate中的clob和blob

阅读更多

转载于http://blog.csdn.net/zmx729618/article/details/51301947

hibernate Annotation中,实体BLOB、CLOB类型的注解与普通的实体属性有些不同,具体操作如下:BLOB类型,类型声明为byte[]:

//简单类型 默认 fetchType 为 EAGER

 private byte[] content;

  注解:
  @Lob
  @Basic(fetch = FetchType.LAZY)
  @Column(name = "CONTENT", columnDefinition = "BLOB",nullable=true)
  public byte[] getContent() {
  return this.content;
  }
  public void setContent(byte[] content) {
  this.content = content;
  }
  CLOB类型,类型声明为String即可:
  private String remark;
  注解:
  @Lob
  @Basic(fetch = FetchType.EAGER)
  @Column(name="REMARK", columnDefinition="CLOB", nullable=true)
  public String getRemark() {
  return this.remark;
  }
  public void setRemark(String recvdocRemark) {
  this.remark = remark;
  }
  按照以上的设置实体类的注解就搞定了。

数据库表如下:

book表

QQ截图20131015201126

id 该表的主键。number类型。
photo 代表图书的图片,blob类型。
description 图书的描述,clob类型。

使用 hibernate3 往 book 表插入Clob,Blob数据

省略hibernate配置文件,实体映射文件和实体类代码,直接帖实现代码:

复制代码
package accp.hibernate;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Blob;
import java.sql.Clob;
import org.hibernate.Hibernate;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public class Client {

    public static void main(String[] args) throws IOException {
        //得到session
        Configuration cfg = new Configuration().configure();
        SessionFactory sf = cfg.buildSessionFactory();
        Session session = sf.openSession();
        //得到图片的blob
        InputStream in = new FileInputStream("F:\\4563123.jpg");
        Blob blob = Hibernate.createBlob(in);
        //得到简介的clob
        Clob clob = Hibernate.createClob("这是一本书和详细描述。#(*&#@¥%(*&@¥)(@#¥#¥");
        //创建图书对象
        Book book = new Book();
        book.setPhoto(blob);
        book.setDescription(clob);
        //存进数据库
        session.beginTransaction();
        session.save(book);
        session.getTransaction().commit();
        session.close();
    }

}
复制代码

    使用hibernate的静态方法createBlob()得到Blob对象。createBlob方法需要一个InputStream对象作为参数。new一个FileInputStream作为它的参数,假设在F盘有一个图片文件名为4563123.jpg。

插入方法2:

bo:

 

[java] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. /** 
  2.  *  
  3.  */  
  4. package test.archive.model;  
  5.   
  6. import java.sql.Blob;  
  7.   
  8. import javax.persistence.Basic;  
  9. import javax.persistence.Column;  
  10. import javax.persistence.Entity;  
  11. import javax.persistence.FetchType;  
  12. import javax.persistence.GeneratedValue;  
  13. import javax.persistence.Id;  
  14. import javax.persistence.Lob;  
  15. import javax.persistence.Table;  
  16.   
  17. import org.hibernate.annotations.GenericGenerator;  
  18.   
  19. /**  
  20.  * @ClassName: BlobTest  
  21.  * @Description: TODO(这里用一句话描述这个类的作用)  
  22.  * @author zhoushun  
  23.  * @date 2013-4-3 上午11:15:38  
  24.  *   
  25.  */  
  26. @Entity  
  27. @Table(name = "T_FUNC_XT_YWDXSM")  
  28. public class BlobTest implements java.io.Serializable {  
  29.       
  30.     /** 
  31.      *  
  32.      */  
  33.     private static final long serialVersionUID = 1L;  
  34.     private byte[] imgs;  
  35.     private String mc;  
  36.     private String id;  
  37.       
  38.       
  39.       
  40.     @Id  
  41.     @GeneratedValue(generator = "system-uuid")  
  42.     @GenericGenerator(name = "system-uuid", strategy = "uuid")  
  43.     @Column(name = "ID")  
  44.     public String getId() {  
  45.         return id;  
  46.     }  
  47.   
  48.     public void setId(String id) {  
  49.         this.id = id;  
  50.     }  
  51.   
  52.     @Column(name = "MC_DX", nullable = true, length = 500)  
  53.     public String getMc() {  
  54.         return mc;  
  55.     }  
  56.   
  57.     public void setMc(String mc) {  
  58.         this.mc = mc;  
  59.     }  
  60.   
  61.     @Lob   
  62.     @Basic(fetch=FetchType.LAZY)   
  63.     @Column(name="IMAGE", columnDefinition="BLOB", nullable=true)  
  64.     public byte[] getImgs() {  
  65.         return imgs;  
  66.     }  
  67.   
  68.     public void setImgs(byte[] imgs) {  
  69.         this.imgs = imgs;  
  70.     }  
  71.   
  72.       
  73.       
  74. }  
[java] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. /** 
  2.  *  
  3.  */  
  4. package test.archive.model;  
  5.   
  6. import java.sql.Blob;  
  7.   
  8. import javax.persistence.Basic;  
  9. import javax.persistence.Column;  
  10. import javax.persistence.Entity;  
  11. import javax.persistence.FetchType;  
  12. import javax.persistence.GeneratedValue;  
  13. import javax.persistence.Id;  
  14. import javax.persistence.Lob;  
  15. import javax.persistence.Table;  
  16.   
  17. import org.hibernate.annotations.GenericGenerator;  
  18.   
  19. /**  
  20.  * @ClassName: BlobTest  
  21.  * @Description: TODO(这里用一句话描述这个类的作用)  
  22.  * @author zhoushun  
  23.  * @date 2013-4-3 上午11:15:38  
  24.  *   
  25.  */  
  26. @Entity  
  27. @Table(name = "T_FUNC_XT_YWDXSM")  
  28. public class BlobTest implements java.io.Serializable {  
  29.       
  30.     /** 
  31.      *  
  32.      */  
  33.     private static final long serialVersionUID = 1L;  
  34.     private byte[] imgs;  
  35.     private String mc;  
  36.     private String id;  
  37.       
  38.       
  39.       
  40.     @Id  
  41.     @GeneratedValue(generator = "system-uuid")  
  42.     @GenericGenerator(name = "system-uuid", strategy = "uuid")  
  43.     @Column(name = "ID")  
  44.     public String getId() {  
  45.         return id;  
  46.     }  
  47.   
  48.     public void setId(String id) {  
  49.         this.id = id;  
  50.     }  
  51.   
  52.     @Column(name = "MC_DX", nullable = true, length = 500)  
  53.     public String getMc() {  
  54.         return mc;  
  55.     }  
  56.   
  57.     public void setMc(String mc) {  
  58.         this.mc = mc;  
  59.     }  
  60.   
  61.     @Lob   
  62.     @Basic(fetch=FetchType.LAZY)   
  63.     @Column(name="IMAGE", columnDefinition="BLOB", nullable=true)  
  64.     public byte[] getImgs() {  
  65.         return imgs;  
  66.     }  
  67.   
  68.     public void setImgs(byte[] imgs) {  
  69.         this.imgs = imgs;  
  70.     }  
  71.   
  72.       
  73.       
  74. }  



 

方法:

 

[java] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. @SuppressWarnings("unchecked")  
  2.     public void blobTest() {  
  3.         BlobTest b = new BlobTest();  
  4.         try {  
  5.             FileInputStream fis = new FileInputStream(  
  6.                     "E:\\Users\\zhoushun\\Pictures\\0fb3c75c9037e36bfbf2c0b5.jpg");  
  7.             byte[] buffer = new byte[fis.available()];  
  8.             fis.read(buffer);  
  9.         fis.close();  
  10.             b.setImgs(buffer);  
  11.             b.setMc("zhoushun");  
  12.             this.h.save(b);  
  13.         } catch (FileNotFoundException e) {  
  14.             // TODO Auto-generated catch block  
  15.             e.printStackTrace();  
  16.         }// 定义文件读入流  
  17.         catch (IOException e) {  
  18.             // TODO Auto-generated catch block  
  19.             e.printStackTrace();  
  20.         }  
  21.   
  22.     }  
[java] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. @SuppressWarnings("unchecked")  
  2.     public void blobTest() {  
  3.         BlobTest b = new BlobTest();  
  4.         try {  
  5.             FileInputStream fis = new FileInputStream(  
  6.                     "E:\\Users\\zhoushun\\Pictures\\0fb3c75c9037e36bfbf2c0b5.jpg");  
  7.             byte[] buffer = new byte[fis.available()];  
  8.             fis.read(buffer);  
  9.         fis.close();  
  10.             b.setImgs(buffer);  
  11.             b.setMc("zhoushun");  
  12.             this.h.save(b);  
  13.         } catch (FileNotFoundException e) {  
  14.             // TODO Auto-generated catch block  
  15.             e.printStackTrace();  
  16.         }// 定义文件读入流  
  17.         catch (IOException e) {  
  18.             // TODO Auto-generated catch block  
  19.             e.printStackTrace();  
  20.         }  
  21.   
  22.     }  



 

    使用hibernate的静态方法createClob()得到Clob对象。createClob方法直接传入字符串即可。

使用 hibernate3 从 book 表取出Clob,Blob数据

省略hibernate配置文件,实体映射文件和实体类代码,直接帖实现代码:

复制代码
 1 package accp.hibernate;
 2 
 3 import java.io.FileOutputStream;
 4 import java.io.IOException;
 5 import java.io.InputStream;
 6 import java.io.OutputStream;
 7 import java.io.Reader;
 8 import java.sql.SQLException;
 9 import org.hibernate.Session;
10 import org.hibernate.SessionFactory;
11 import org.hibernate.cfg.Configuration;
12 
13 public class Client {
14 
15     public static void main(String[] args) throws IOException, SQLException {
16         //得到session
17         Configuration cfg = new Configuration().configure();
18         SessionFactory sf = cfg.buildSessionFactory();
19         Session session = sf.openSession();
20 
21         //从数据库里取值
22         session.beginTransaction();
23         Book book = (Book)session.get(Book.class,22);
24         session.getTransaction().commit();
25         session.close();
26         //把简历打印到控制台
27         Reader reader = book.getDescription().getCharacterStream();
28         int i = reader.read();
29         while(i!=-1){
30             System.out.print((char)i);
31             i=reader.read();
32         }
33         reader.close();
34         //把图片拷贝到D盘
35         InputStream in = book.getPhoto().getBinaryStream();
36         OutputStream out=new FileOutputStream("d:\\out.jpg");
37         byte[] b=new byte[1024];    
38         while((i=in.read(b))!=-1){
39             out.write(b);
40         }
41         out.close();
42         in.close();
43     }
44 
45 }
复制代码

    21-25行代码把数据从数据库中取出,封装到book对象中。

    为了验证取值是否成功,第26-33行代码把Clob对象的内容打印到控制台,通过Clob对象的getCharacterStream()方法得到包含 CLOB 数据的 Java.io.Reader 对象,然后通过操作Reader对象把内容输出。

    第34-42行代码把Blob对象包含的内容拷贝到d:\out.jpg文件中。通过Blob对象的getBinaryStream()方法得到包含 BLOB 数据的流,通过操作流把内容拷贝。

使用 hibernate4 往 book 表插入Clob,Blob数据

hibernate4相比hibernate3要复杂一些,如下:

复制代码
//省略import...........

public class Client {

    public static void main(String[] args) throws IOException {
        //得到session
        Configuration cfg = new Configuration().configure();
        ServiceRegistry  sr = new ServiceRegistryBuilder().applySettings(cfg.getProperties()).buildServiceRegistry(); 
        SessionFactory sf = cfg.buildSessionFactory(sr);
        Session session = sf.openSession();
        //得到LobHelper
        LobHelper lobHelper = session.getLobHelper();
        //得到图片的blob
        InputStream in = new FileInputStream("F:\\4563123.jpg");
        Blob blob = lobHelper.createBlob(in, in.available());
        //得到简介的clob
        Clob clob = lobHelper.createClob("这是一本书。#(*&#@¥%(*&@¥)(@#¥#¥");
        //创建图书对象
        Book book = new Book();
        book.setPhoto(blob);
        book.setDescription(clob);
        //存进数据库
        session.beginTransaction();
        session.save(book);
        session.getTransaction().commit();
        session.close();
    }

}
复制代码

      hibernate4中得到session对象的方法和hibernate3中略有不同,但用hibernate3的写法也是可以的。

      在hibernate4中 hibernate.createBlob() 方法和 hibernate.createClob() 已经过时,取而代之的是LobHelper.createBlob()和lobHelper.createClob()。可以通过session.getLobHelper();得到LobHerlper对象。

取值 和hibernate3 基本一样,这里不再演示!

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics