'How to edit and save multiple records in database from spring boot

My problem is simple and straight forward. I want to edit multiple records and save them in database. For editing a single record, I have used following statement in JpaRepository

DatabaseEntity findByAbcId(Integer abcId);

Here, I am trying to fetch a record from Mysql database table DatabaseEntity with respect to its column named abcId which is a foreign key of another table named ABC.

In my service class, I get this record, set its attributes and simply save it back in database like:

//Getting an existing record from database:
//Giving hardcoded value just for understanding
DatabaseEntity databaseEntity = databaseEntityRepository.findByAbcId(106);

//Setting edited fields into Model object. Except for its own id and abcId(foreign key)
databaseEntity.setCol1(value);
databaseEntity.setCol2(value);
databaseEntity.setCol3(value);

databaseEntityRepository.save(databaseEntity);

The above code will get a record and save its edited version into the database.

Now lets take a similar scenario but this time, the database is retrieving multiple records. from the database. Suppose multiple records are present against abcId column in my table. The changes in my code will be:

//Storing the result in the list as there are multiple records stored against 106
List<DatabaseEntity> databaseEntity = databaseEntityRepository.findByAbcId(106);

//What should I code here?

Now I am confused how to set values in multiple fields in a single go from spring boot. The values that I need to edit are also in another list and I tried iterating over both lists and change the database records accordingly but my approach is not a good one

//List of those records which I have edited
if(newRecordsList != null) {
                 //Using atomic Integer because only that can be changed within lambda expression
                 AtomicInteger outerLoopCounter = new AtomicInteger(0);
                 List<DatabaseEntity> databaseEntity = databaseEntityRepository.findByAbcId(Abc.getId());
                        databaseEntity.forEach(obj -> {
                           AtomicInteger innerLoopCounter = new AtomicInteger(0);
                            newRecordsList.forEach(newRecordsListObj -> {
                                //condition so that first record is updated according to the updated first record and other changes are updated accordingly.
                                if(outerLoopCounter.get() == innerLoopCounter.get()) {
                                    obj.setName(newRecordsListObj.getName());
                                    obj.setCondition(newRecordsListObj.getCondition());
                                    obj.setValue(newRecordsListObj.getValue());
                                    databaseEntityRepository.save(obj);
                                }
                               innerLoopCounter.incrementAndGet();
                            });
                            outerLoopCounter.incrementAndGet();
                        });
}

I know this is a very bad approach and I want to update my logic. So please help me to update these multiple records inside database.

Thanks in advance



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source