passing list on Insert command using Room duplicating the values in Android

Viewed 752

I am passing countryList of Country to save it in database using Room. its saving but its duplicating the values and OnConflict Replace strategy not working.

AppDatabase.getAppDatabase(getApplicationContext()).countryDao().insertAllList(countryList);

list values are being duplicated even though i am passing the below replace strategy.

@Insert(onConflict = OnConflictStrategy.REPLACE)

Here is CountryDao

@Dao
public interface CountryDao {

@Query("SELECT * FROM country")
List<Country> getAllCountries();

@Insert(onConflict = OnConflictStrategy.REPLACE)
void insertAllList(List<Country> countries);

}

Country Object:

 @Entity(tableName = "country")
    public class Country {
    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    @PrimaryKey(autoGenerate = true)
    private int id;
    private long countryId;
    private String countryName;

    public long getCountryId() {
        return countryId;
    }

    public void setCountryId(long countryId) {
        this.countryId = countryId;
    }

    public String getCountryName() {
        return countryName;
    }

    public void setCountryName(String countryName) {
        this.countryName = countryName;
    }
}
1 Answers
Related