Best way to insert a good amount of records in hibernate

前端 未结 5 1407
甜味超标
甜味超标 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:08

    Just open your session and transaction.

    Add all elements in the save of the session.

    Then commit the transaction.

    //Remember to effective handler errors
    public void saveAll(List list) throws Exception{
    Session s = HibernateUtil.openSession();
    Transaction tx = s.beginTransaction();
    for(Object obj : list)
     s.save(obj);
    tx.commit();
    s.flush();
    s.close();
    }
    
        

    提交回复
    热议问题