AI写作智能体 自主规划任务,支持联网查询和网页读取,多模态高效创作各类分析报告、商业计划、营销方案、教学内容等。 广告
# 13.1\. 批量插入(Batch inserts) 如果要将很多对象持久化,你必须通过经常的调用 `flush()` 以及稍后调用 `clear()` 来控制第一级缓存的大小。 ``` Session session = sessionFactory.openSession(); Transaction tx = session.beginTransaction(); for ( int i=0; i<100000; i++ ) { Customer customer = new Customer(.....); session.save(customer); if ( i % 20 == 0 ) { //20, same as the JDBC batch size //20,与JDBC批量设置相同 //flush a batch of inserts and release memory: //将本批插入的对象立即写入数据库并释放内存 session.flush(); session.clear(); } } tx.commit(); session.close(); ```