1

Assume that we have a list of user details, and we want to add it to the database, but I want that if there are some users in the list that could have already been added but some aren't. So is there any way we can do it with

dao.saveAll(list);

without first checking in db for data

  • It's certainly possible although I'm not sure about the complexity of such a solution. This might help get started: https://stackoverflow.com/questions/56274648/spring-data-insert-data-when-entry-not-yet-exists - Note that it's probably easier to do that yourself: load a set of ids that already exist in the DB, filter your list by removing those that already exist (maybe create a copy of the list during that process) and finally save the filtered list. - That could still fail due to concurrent DB updates though. Alternatively use a batch insert where indiviual ones are allowed to fail. – Thomas Sep 01 '22 at 06:53

1 Answers1

0

If the entity user has an id, JPA will perform an update on the record. if id is null or id doesn't exit the database, JPA will create on the record.