HQL Batch Update, Insert
1.Batch Insert
Session session = sessionFactory.openSession(); Transaction tx = session.beginTransaction(); for ( int i=0; i<100000; i++ ) { Customer customer = new Customer(.....); session.save(customer); } tx.commit(); session.close();
2.Batch Update
Session session = sessionFactory.openSession(); Transaction tx = session.beginTransaction(); ScrollableResults customers = session.getNamedQuery("GetCustomers") .setCacheMode(CacheMode.IGNORE) .scroll(ScrollMode.FORWARD_ONLY); int count=0; while ( customers.next() ) { Customer customer = (Customer) customers.get(0); customer.updateStuff(...); if ( ++count % 20 == 0 ) { //flush a batch of updates and release memory: session.flush(); session.clear(); } } tx.commit(); session.close();
本文介绍了使用Hibernate Query Language (HQL)进行批量插入和批量更新操作的方法。具体包括通过SessionFactory创建Session并开启事务,利用循环进行批量插入,以及滚动查询结合批量更新的方式提高内存管理和更新效率。
4444

被折叠的 条评论
为什么被折叠?



