How to get score of updated records in spring jpa data?

I am using spring jpa data with hibernate as a jpa continuity provider.

I use my own queries in my application. There are several update requests, and I would like to get the actual number of records updated when the update request was executed. Is there a way in spring jpa data for this?

I am currently following the approach below;

@Modifying @Query(value="update table x set x_provision = ?1 where x_id = ?2", nativeQuery=true) int updateProvision(Integer provision, Integer id); 

@Transactional is added at the service level.

The problem is that when the table is updated, I get the score as 1. But there are cases when not a single row is updated. In this case, I also get the score as 1. But I would like to get the actual number of updated records, which is sometimes 0.

Can someone tell me if I am doing something wrong here?

+6
source share
1 answer

If you look at the ModifyingExecutor that is used to execute this query

 @Override protected Object doExecute(AbstractJpaQuery query, Object[] values) { int result = query.createQuery(values).executeUpdate(); if (em != null) { em.clear(); } return result; } 

Then you see that it delegates only to the embedded JPA infrastructure to return the number of updated items.

Thus, this may be related to the database configuration used or the ORM configuration, in addition, the query you wrote should return the correct result.

+3
source

Source: https://habr.com/ru/post/955439/


All Articles