Posted on 2006-07-21 15:53
波波 阅读(355)
评论(0) 编辑 收藏
下面是一个用Hibernate实现对数据库进行批量操作的示例
public class AddMoreWords {
private static String type = "idiom";
/**
* @param args
*/
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
Session session = HibernateSessionFactory.currentSession();
Transaction txc = session.beginTransaction();
for(int i=0;i<1000;++i){
WordLib word = new WordLib();
word.setChn("mm"+(i+1));
word.setTime(new Date());
word.setType(type);
session.save(word);
if((i+1)%25 == 0){
session.flush();
session.clear();
}
}
// WordLib word = new WordLib();
// word.setChn("a");
// word.setTime(new Date());
// word.setType(type);
// session.save(word);
txc.commit();
HibernateSessionFactory.closeSession();
session.flush();
session.close();
}
}