0
{
events:
[
{"id": "1"}
{"id": "2"}
]
attributes:
[
{
"field": "field1"
"value": "value1"
}
{
"field": "field2"
"value": "value2"
}
]
}

For the above input i would like to update column "field": "field1" of row "id": "1" with "value": "value1" .

But in case of partial error I should send back a list of failed id .

How to get this list of failed id in JPA/Hibernate bulk update ?

One way to do this is parse the list one by one and do a transactional update for each id one by one , in case of exception in DAO call, catch the exception and store this particular id in the partial failed list .

But this feels like a brute force way of doing it, does JPA provide a better way to do bulk update and return the list of failed id ?

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Sourabh
  • 119
  • 1
  • 8

1 Answers1

1

Creating a transaction for every update is not efficient. So as you correctly said, you should use batching. Here is a good article on how to do it. You will find there that only one transaction is created before updates/inserts.

In case the whole batch fails, it's possible to find which statement failed in the batch. java.sql.BatchUpdateException provides you a method for this.

So i'd advise you to have batch updates/inserts block of code, with catch(BatchUpdateException), where you can process it individually as a fallback.

Dmitrii Bocharov
  • 872
  • 6
  • 21