批量插入(Batch inserts)
如果要將很多對(duì)象持久化,你必須通過(guò)經(jīng)常的調(diào)用 flush() 以及稍后調(diào)用 clear() 來(lái)控制第一級(jí)緩存的大小。
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批量設(shè)置相同
//flush a batch of inserts and release memory:
//將本批插入的對(duì)象立即寫(xiě)入數(shù)據(jù)庫(kù)并釋放內(nèi)存
session.flush();
session.clear();
}
}
tx.commit();
session.close();
如果要將很多對(duì)象持久化,你必須通過(guò)經(jīng)常的調(diào)用 flush() 以及稍后調(diào)用 clear() 來(lái)控制第一級(jí)緩存的大小。
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批量設(shè)置相同
//flush a batch of inserts and release memory:
//將本批插入的對(duì)象立即寫(xiě)入數(shù)據(jù)庫(kù)并釋放內(nèi)存
session.flush();
session.clear();
}
}
tx.commit();
session.close();