aspose file tools*
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
Author

Hibernate

Priyadarshini Batkha
Greenhorn

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.

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

Joined: Oct 13, 2005
Posts: 40052
    
  28
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

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

Joined: Mar 07, 2010
Posts: 1680
    
    7

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.


[How To Ask Questions][Read before you PM me]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Hibernate