日期:2014-05-16  浏览次数:20460 次

JPA 批量插入

EntityManagerFactory emf = Persistence.createEntityManagerFactory("DataSourceDB2");
??????????? EntityManager em
= emf.createEntityManager();
??????????? EntityTransaction et
= em.getTransaction();
??????????? et.begin();?
???????????
??????????? Session session
= (Session) em.getDelegate();
??????????? session.setFlushMode(FlushMode.MANUAL);?

?


??????????? int batchSize = 100;
???????????

int i = 0;
???????????
for(Market mk : mkts)
??????????? {?
??????????????? session.save(mk );
??????????????? i
++;
???????????????
if( i % batchSize? == 0 ){

????????????????????? session.flush();
?????????????????? session.clear();
??????????????? }
??????????? }?

??????????? et.commit();