public static Session getSession(){
Resource resource = new ClassPathResource("*.xml"); // hibernate.cfg.xml,此处为hibernate核心配置文件
BeanFactory factory = new XmlBeanFactory(resource);
SessionFactory sessionFactory = (SessionFactory) factory
.getBean("mySessionFactory");
Session session = sessionFactory.openSession();
return session;
}
public void insertData(){
Session session = getSession();
Transaction trans = session.beginTransaction();
ErrorList<Error> errorList = getErrorList(); // 假设此处获取到error列表
for(int i = 1; i <= errorList.getSize(); i++){
// Error error = new Error(); // 实体类
Error error = errorList.get(i - 1);
session.save(error);
// 以5000为批量单位提交
if (i % 5000 == 0) {
session.flush();
session.clear();
trans.commit();
trans = session.beginTransaction();
}
session.flush();
session.clear();
trans.commit();
session.close();}
}