批量插入(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();
如果要將很多對象持久化,你必須通過經常的調用 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();