Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Junilu Lacar
  • Martin Vashko
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Scott Selikoff
  • salvin francis
  • Piet Souris

Hibernate

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

Service:
used annotation @Transactional

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

Dao:
public T update(T entity){
entityManager.merge(entity);
}

Please provide me a simple way to perform batch update.



Thanks in advance!
 
Marshal
Posts: 66486
251
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Its hibernate in spring actually..
 
Bartender
Posts: 1682
7
Android Mac OS X IntelliJ IDE Spring Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
https://docs.jboss.org/hibernate/core/4.0/devguide/en-US/html/ch04.html

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.
 
I am mighty! And this is a mighty small ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!