javastrutsapache-torque

Delete all rows in database using struts and torque


I need to delete a more than one row in database.

The data is in Tariff table which include another table's (vehicle) primary key.

Suppose if I delete a vehicle table data, the data which one is related to that particular id in tariff table also is deleted.

I tried, if the tariff contains more than two rows related to vehicle that I need to delete, and delete the first one in tariff table others are not deleted.

Here is a code:

Vehicle vehicle = VehiclePeer.doSelectFirst(c);
if (vehicleName != null && !vehicleName.equals(""))
{                   
  c.clear();
  c.add(TariffPeer.VEHICLE_ID, vehicle.getId());
  Tariff tariff = (Tariff) TariffPeer.doSelectAll();
  if (tariff != null && !tariff.equals(""))
  {
    TariffParamsPeer.doDelete(c.add(TariffParamsPeer.TARIFF_ID, tariff.getId()));
    TariffPeer.doDelete(tariff);
  }
}

Please, help me to sort out this, thanks in advance.


Solution

  • You need to write a method that retun a set of Tariff for the given vheicleId. And rewrite the code as follows

    Vehicle vehicle = VehiclePeer.doSelectFirst(c);
    if (vehicleName != null && !vehicleName.equals("")) {                    
      c.clear();
      c.add(TariffPeer.VEHICLE_ID,vehicle.getId());
      Set<Tariff> tarrifSet = TariffPeer.doSelectAll(vehicle.getId());
      for (Tariff tariff: tarrifSet) { 
        if (tariff !=null && !tariff.equals("")) {       
          c.add(TariffParamsPeer.TARIFF_ID, tariff.getId());
          TariffPeer.doDelete(tariff);
        }
      }
    }