I was working on saving data that is in one to many relationship. Like Country has many states as the image below
Here is the model the db table is build up on
public class Country {
private int countryId;
private String countryName;
private List<State> states;
}
and here is the country and state saving code.
public void saveToDb(List<Country> countries) {
for(Country country: countries){
saveCountry(country);
List<State> states = country.getStates();
for(State state :states)
saveState(state);
}
}
Knowing how expensive nested loops can be, I was wondering if there was an efficient way of saving that data without using nested loops. Any help would be very much appreciated