nauman nauman - 1 year ago 83
Android Question

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

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 Ojbect

`@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;
}


}`

kkk kkk
Answer Source

You must have different id for each Country and assign it manually, otherwise each element will have id=0 and override another.

Try without autogenerate key

@PrimaryKey(autoGenerate = false)
private int id;
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download