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     }

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值