在android上删除使用ormlite?

Maj*_*jid 19 android ormlite foreign-collection

我有一个Client bean,

@DatabaseField(columnName = "client_id",generatedId = true,useGetSet = true)
private Integer clientId;
@DatabaseField(columnName = "client_nom",useGetSet = true)
private String clientNom;
@DatabaseField(columnName = "city_id",foreign = true,useGetSet = true)
private City city;
Run Code Online (Sandbox Code Playgroud)

和一个城市豆,

@DatabaseField(columnName = "city_id",generatedId = true,useGetSet = true)
private Integer cityId;
@DatabaseField(columnName = "city_name",useGetSet = true)
private String cityName;
@ForeignCollectionField
private ForeignCollection<Client> clientList;
Run Code Online (Sandbox Code Playgroud)

那些豆只是一个例子,但是我想说,我想在删除城市时删除所有拥有外国城市cityId的客户.

请问怎么可能?

Gra*_*ray 51

ORMLite不支持级联删除@Majid.这目前超出了它认为的"精简版".如果删除city则需要clients手动删除.

确保这一点的一种方法是使用一个CityDao类来覆盖该delete()方法并同时发出删除ClientDao.就像是:

public class CityDao extends BaseDaoImpl<City, Integer> {
    private ClientDao clientDao;
    public CityDao(ConnectionSource cs, ClientDao clientDao) {
        super(cs, City.class);
        this.clientDao = clientDao;
    }
    ...
    @Override
    public int delete(City city) {
        // first delete the clients that match the city's id
        DeleteBuilder db = clientDao.deleteBuilder();
        db.where().eq("city_id", city.getId());
        clientDao.delete(db.prepare());
        // then call the super to delete the city
        return super.delete(city);
    }
    ...
}
Run Code Online (Sandbox Code Playgroud)