Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hibernate batch update using HQL Query

I'm using Hibernate for my ORM layer. I'm try to run batch of HQL queries in one transaction (I cannot use session.update). The issue is that even the transaction.commit() is at the end of the loop, the update queries run one by one. Is there a way to run multiple HQL queries in one transaction?

public void updateItems() {
    t = session.beginTransaction();
    for (int i = 0; i < itemList.size(); i++) {
        Query q = createUpdateQuery(session, itemList.get(i));      
        q.executeUpdate(); //updating one by one, and not waiting for transaction commit
    }
    t.commit();
}



Query createUpdateQuery(Session session, Item item) {
    Query q = session.createQuery(
                "Update Item i set i.notes=:notes, i.time=:time, i.counter=:counter, i.status=:status Where i.id=:id and i.time=:time");

    q.setParameter("time", item.getTime());
    q.setParameter("status", item.getStatus());
    q.setParameter("notes", item.getNotes());
    q.setParameter("id", item.getId());
    return q;
}

Appreciate any help.

like image 429
Lasti Avatar asked Apr 20 '16 03:04

Lasti


People also ask

What is batch update in Hibernate?

1. Overview. In this tutorial, we'll learn how we can batch insert and update entities using Hibernate/JPA. Batching allows us to send a group of SQL statements to the database in a single network call. This way, we can optimize the network and memory usage of our application.

Is HQL faster than SQL?

You can select only certain columns with HQL, too, for example you can use select column from table in HQL. Native SQL is not necessarily faster than HQL. HQL finally also is translated into SQL (you can see the generated statement when running the application with the show_sql property set to true).

Does Hibernate support union HQL query?

You may note that hibernate's HQL does not allow unions, as this does not reconcile with the object oriented approach of HQL. However, as this can be a common technique to apply, a programmatic union has been provided in the Oracle Utilities Application Framework.

Is HQL and JPQL same?

The Hibernate Query Language (HQL) and Java Persistence Query Language (JPQL) are both object model focused query languages similar in nature to SQL. JPQL is a heavily-inspired-by subset of HQL. A JPQL query is always a valid HQL query, the reverse is not true however.


1 Answers

You are using a database transaction to enroll all your statements, but I think you want to use batch updates.

Just add the following configuration property:

<property name="hibernate.jdbc.batch_size" value="10"/>

Even so, I think you should use Hibernate to manage the insert/update/delete statements, as you should only focus on entity state transitions. The dirty checking mechanism can automatically detect entities that have been modified, and Hibernate can generate the update statement for you, which is much more convenient.

like image 51
Vlad Mihalcea Avatar answered Oct 23 '22 21:10

Vlad Mihalcea