hibernate批量插入数据

来源:互联网 发布:php post接口 编辑:程序博客网 时间:2024/05/22 12:14
  1. public void testInserBatch() {
  2.    Session session = sf.openSession();  
  3.     session.beginTransaction();  
  4.     //每次插入20条数据 
  5.     for(int i=0; i<1000; i++) {  
  6.         Category c = new Category();  
  7.         c.setName("test" + i);  
  8.         session.save(c);  
  9.         if (i%20==0) {  
  10.             session.flush();  
  11.         }  
  12.     }  
  13.           
  14.     session.getTransaction().commit();  
  15.     session.close(); 

0 0