hibernate批量插入数据

hibernate采用缓存机制存储数据,如果大量数据提交时 缓存不够就会报错,所以在处理批量插入数据时

要定期的清空hibernate缓存。

例如以下代码:

1- 采取分批提交策略。

2- 如果不写commit的话默认事务最后自动提交,也就是只提交一次(第二种写法)

3- 可以直接调用JDBC addBatch方法进行处理;

第一种写法

public void addBatchChannelFundRelation(Tchannelfundrelation vpo,int currentCount)
    throws DAOException {
try {
   Session hs = HibernateUtil.currentSession();
   Transaction tr = hs.beginTransaction();
hs.save(vpo);
hs.flush(); 
if (currentCount%50==0) { //每五十条记录提交一次 
tr.commit();
hs.clear();   //清理SESSION内存
}
} catch (HibernateException he) {
   throw new DAOException (he.getMessage(),he);
}
  第二种写法

 也可以:最后一次提交

public void addBatchChannelFundRelation(Tchannelfundrelation vpo,int currentCount)
    throws DAOException {
try {
    Session hs = HibernateUtil.currentSession();
    Transaction tr = hs.beginTransaction();
hs.save(vpo);

if (currentCount%50==0) { //每五十条记录提交一次 
hs.flush(); 
hs.clear();   //清理SESSION内存
}
} catch (HibernateException he) {
    throw new DAOException (he.getMessage(),he);
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值