hibernate 大对象映射

1、 在pojo类中 用Blob类和Clob

 

1 public class Student {
2     private int id;
3     private String name;
4     private int age;
5     //存放大数据  可以存放4G的内容
6     private Blob image;
7     private Clob introduce;
8 //省略get/set
9     }

 

2、 在hbm文件中 需指定对应类型

 

 1 <hibernate-mapping package="cn.siggy.pojo">
 2     <class name="Student">
 3         <id name="id">
 4             <generator class="native"></generator>
 5         </id>
 6         <property name="name"/>
 7         <property name="age"/>
 8         <property name="image" type="java.sql.Blob"/>
 9         <property name="introduce" type="java.sql.Clob"/>
10     </class>
11 </hibernate-mapping>

 

3、 构造对象 测试

 1 @Test
 2     public void testSave() throws HibernateException, SerialException, SQLException{
 3         Session session = null;
 4         Transaction tx = null;
 5         try{
 6             session = HibernateUtil.getSession();
 7             tx = session.beginTransaction();
 8             Student stu = new Student();
 9             stu.setName("尹志平");
10             stu.setAge(23);
11             
12             Blob blob = new SerialBlob("ttt".getBytes());
13             Clob clob = new SerialClob("sss".toCharArray());
14             stu.setImage(blob);
15             stu.setIntroduce(clob);
16             session.save(stu);
17             
18             tx.commit();
19             
20         }catch (HibernateException e) {
21             if(tx!=null)
22                 tx.rollback();
23             e.printStackTrace();
24             throw e;
25         }finally{
26             HibernateUtil.closeSession();
27         }
28     }

 

 

原文地址:https://www.cnblogs.com/jiangjianzhu/p/5546355.html