删除使用ormlite在Android? [英] Deleting using ormlite on android?
问题描述
我有一个客户端Bean,
I have a 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;
和一个城市的bean,
and a City bean ,
@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;
这些豆子都只是一个例子,但让我们说,我想删除全部删除城市,当有外国城市cityId客户。
Those beans are just an example but let's say , I want to delete all the clients having as foreign city cityId when deleting a city.
这怎么可能吗?
推荐答案
ORMLite 不支持级联删除@Majid。这就是目前它认为是精简版之外。如果删除城市
,那么你需要手工删除客户
。
ORMLite does not support cascading deletes @Majid. That is currently outside of what it considers to be "lite". If you delete the city
then you need to delete the clients
by hand.
要确保这将是有一个 CityDao
单向类覆盖删除()
方法和问题在同一时间通过 ClientDao
的删除。是这样的:
One way to ensure this would be to have a CityDao
class that overrides the delete()
method and issues the delete through the ClientDao
at the same time. Something like:
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);
}
...
}
这篇关于删除使用ormlite在Android?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!