下面是一个用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();
}
}