Best way to insert a good amount of records in hibernate

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

    Best is to use StatelessSessions. Consider the following example from (http://docs.jboss.org/hibernate/orm/3.3/reference/en-US/html/batch.html):

    StatelessSession session = sessionFactory.openStatelessSession();
    Transaction tx = session.beginTransaction();
    
    ScrollableResults customers = session.getNamedQuery("GetCustomers")
        .scroll(ScrollMode.FORWARD_ONLY);
    while ( customers.next() ) {
        Customer customer = (Customer) customers.get(0);
        customer.updateStuff(...);
        session.update(customer);
    }
    
    tx.commit();
    session.close();
    

提交回复
热议问题