File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Object Relational Mapping and the fly likes Hibernate Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Hibernate" Watch "Hibernate" New topic


Priyadarshini Batkha

Joined: Dec 07, 2013
Posts: 2
How to update a list of records in DB using Entity Manager ? I think it could be done using batch insert but just wanted to know the procedure. For Ex. For now i use a for loop for hitting the database every time for updating a record and doing EntityManager.merge.

I am new to Spring MVC and not much idea with JPA as well. All I want to do is to update a list of records and its working well when I loop through the list and call update on DAO.

But I dnt want to perform 100 update/insert operations on DB round trips.

used annotation @Transactional

List<MyEntity> list = form.getList();
for(MyEntity e : list){

public T update(T entity){

Please provide me a simple way to perform batch update.

Thanks in advance!
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 46375
Welcome to the Ranch

Do you mean Spring or Hibernate? I shall move this discussion to one of those fora, because that is where we usually discuss such things. I might get it in the wrong one, however
Priyadarshini Batkha

Joined: Dec 07, 2013
Posts: 2
Its hibernate in spring actually..
Bill Gorder

Joined: Mar 07, 2010
Posts: 1682

Welcome to the Ranch. If you were just looking to do a bulk update you could do it with a JPQL update statement

Of course when you do this you are going around the entity manager factory so you might want to clear your persistence context by calling clear() on the entity manager afterwards.

For batching there is no good way to do this with JPA that I know of. You are going to have to drop down to provider specific stuff. For Hibernate see the below link.

Of course your next question is going to be how do I get the Hibernate Session if I am using JPA?

Try this (assuming you are using JPA2):

Also I am moving this to the ORM forums.

[How To Ask Questions][Read before you PM me]
I agree. Here's the link:
subject: Hibernate
It's not a secret anymore!