Use of Batch updates in HQL by R4R Team

Batch updates: For retrieving and updating data, need to do the same ideas and same will apply. In addition, need to use scroll() to take advantage of server-side cursors for queries that return many rows of data.


By the help of the simple code we can understand what need to do:


Session session = sessionFactory.openSession();

Transaction tx = session.beginTransaction();

ScrollableResults customers = session.getNamedQuery("GetCustomers")

    .setCacheMode(CacheMode.IGNORE)

    .scroll(ScrollMode.FORWARD_ONLY);

int count=0;

while ( customers.next() ) {

    Customer customer = (Customer) customers.get(0);

    customer.updateStuff(...);

    if ( ++count % 20 == 0 ) {

        //flush a batch of updates and release memory:

        session.flush();

        session.clear();

    }

}

tx.commit();

session.close();

Leave a Comment:
Search
Categories
R4R Team
R4Rin Top Tutorials are Core Java,Hibernate ,Spring,Sturts.The content on R4R.in website is done by expert team not only with the help of books but along with the strong professional knowledge in all context like coding,designing, marketing,etc!