Best way to insert a good amount of records in hibernate

前端 未结 5 1409
甜味超标
甜味超标 2020-12-29 10:53

I\'m using hibernate + play! framework at work, is there a \"best practice\" on inserting a good amount of records using hibernate? They are around 6,000 to 10,000 per text

5条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-29 11:21

    From *Java Persistence and Hibernate" (Manning) and following a comment from Pangea, use a stateless session (which doesn't have a persistence context cache) :

    StatelessSession session = sessionFactory.openStatelessSession();
    Transaction tx = session.beginTransaction();
    for ( int i=0; i<100000; i++ ) {
        Item item = new Item(...);
        session.insert(item);
    }
    tx.commit();
    session.close();
    

提交回复
热议问题