19

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;

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;

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.

How is that possible please ?

Gray
  • 115,027
  • 24
  • 293
  • 354
Majid
  • 199
  • 1
  • 1
  • 3

2 Answers2

51

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.

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);
    }
    ...
}
abarnert
  • 354,177
  • 51
  • 601
  • 671
Gray
  • 115,027
  • 24
  • 293
  • 354
  • 1
    Can you please give an example of how would I instantiate CityDao. For instance in my app I have "private ArtistDao artistDao = null" and "artistDao = new ArtistDao(Artist.class);". I don't know how to migrate to custom extended class, I get cast exceptions and I don't know how and where to supply the connection source. – Speed Demon Feb 27 '14 at 12:00
  • I don't understand @SpeedDemon. Your `ArtistDao` must be a concrete class if you instantiate it. The `CityDao` in my post shows how to extend `BaseDaoImpl`. – Gray Feb 27 '14 at 14:16
  • I guess your constructor has to throw SQLException – Piotr Jul 08 '14 at 02:17
4

To implement cascading while using ORMLite on Android you need to enable foreign key restraints as described here:

(API level > 16)

@Override
public void onOpen(SQLiteDatabase db){
    super.onOpen(db);
    if (!db.isReadOnly()){
        db.setForeignKeyConstraintsEnabled(true);
    }
}

For API level < 16 please read: Foreign key constraints in Android using SQLite? on Delete cascade

Then use columnDefinition annotation to define cascading deletes. Ex:

@DatabaseField(foreign = true,
columnDefinition = "integer references my_table(id) on delete cascade")
private MyTable table;

This is assuming the table/object name is "my_table", as described here: Creating foreign key constraints in ORMLite under SQLite

Community
  • 1
  • 1