• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Hibernate

 
Priyadarshini Batkha
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!
 
Campbell Ritchie
Sheriff
Pie
Posts: 49823
69
  • 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..
 
Bill Gorder
Bartender
Posts: 1682
7
Android IntelliJ IDE Linux Mac OS X Spring
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic