我需要使用hibernate在数据库中插入大量数据,我正在查看来自hibernate的批量插入,我使用的是类似于手册中的示例:
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
//flush a batch of inserts and release memory:
session.flush();
session.clear();
}
}
tx.commit();
session.close();
Run Code Online (Sandbox Code Playgroud)
但我发现flush不会在数据库上写入数据.阅读它,如果代码在事务中,那么在事务执行提交之前不会将任何内容提交给数据库.
那么需要使用flush/clear是什么?似乎没用,如果数据没有写在数据库上,那么它们就在内存中.
我如何强制hibernate在数据库中写入数据?
谢谢