Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring data JPA update multiple rows

purpose: update some columns of same table by given list of primary keys and return ordered int[] to indicate if a row update is successful or not

query (not native):

update MyEntity e set e.col_1 = :v1, e.col_2 = :v2 where e.id in :ids

Spring Data JPA method:

@Modifying(clearAutomatically = true)
@Transactional
public int updateCols(@Param("v1") String v1, @Param("v2") String v2, @Param("ids") List<Long> idList);

Spring JpaTransactionManager is used

Questions: 1) With Spring Data JPA only, how to do that? (customize repository not an option due to large existing codes), we want to achieve effects equivalent to JDBC batch update with Spring data JPA only.

2) Is it possible that spring data JAP will return an integer by the update method above that is less then the input list size? If that could happen, how do we know which one on my input list failed? In any order?

3) Noticed that after the above method successfully finished, database rows not being updated at all. How could we force it synchronized with database with spring data jpa?

4) If the entity update not flushed by spring data JPA, then even we update one by one instead of in a batch, still we will not see database changes? Thanks in advance

like image 504
john Avatar asked Sep 28 '16 02:09

john


People also ask

How do I execute a modifying query in spring data JPA?

When working with JPA’s EntityManager, the required code to execute a modifying query slightly differs from the code that executes a SELECT statement. Because of that, you need to tell Spring Data JPA that you provided a modifying statement as the value of the @Query annotation.

What can spring data JPA do for You?

When using Spring Data JPA, most developers are used to letting Spring handle almost all database operations. That’s especially the case for all update operations. Thanks to JPA’s entity mappings and the managed lifecycle of all entity objects, you only need to change an attribute of an entity object. Everything else happens automatically.

How do I update an entity in JPA?

Updating Entities with Update Query in Spring Data JPA. With object-relational mapping (ORM) tools such as Hibernate, one usually modifies entities by first fetching them from the database, modifying some fields, and then persisting the entities again. This is a good approach in many cases, but in some scenarios this may not be desirable.

What are @modifying and @query annotations in JPA?

In fact, JPA defines two annotations, @Modifying and @Query, that allow us to write our update statement explicitly. We can now tell our application how to behave during an update, without leaving the burden on the ORM. Let's add our custom update method in the repository:


1 Answers

First of all, AFAIK Spring Data JPA can't exist on its own, it's just a thing build upon bare JPA. It needs to use Hibernate or any other JPA provider under the hood.

https://github.com/colonder/Spring-Hibernate-JavaFX-Invoices

Have a look at my repository. It's Hibernate + Spring Boot + Spring Data JPA + Java FX 8, but I think it'll help you a little. My repo follows MVC pattern and uses Spring transaction manager, just like yours. As regards question 3 and 4 AFAIK Hibernate (because that's what Spring Data JPA use in my case as a provider) updates rows in the DB, not in the app. The problem should be that updated rows in the DB are not visible in the app, not the opposite.

like image 127
Colonder Avatar answered Sep 20 '22 22:09

Colonder