Mein Problem ist folgende dass ich in OwnerController delete Methode implementieren möchte und wenn ich import com.vb.model.User ; möchte habe ich Fehlermeldung dass ich das löschen muss. mir macht noch Schwierigkeit 53.Zeile in der OwnerController und verlangt cast nachdem ich das Akzeptiere bekomme ich folgende Zeile com.vb.model.User owner = ownerDao.getUserByUsername(username);
. Bitte bitte hilft mir. in AdminController hat reibungslos funktioniert
public ResponseEntity<String> deleteAdmin(@RequestParam String username){
User admin = adminDao.getUserByUsername(username);
if(admin != null && admin instanceof Admin){
adminDao.deleteUser(admin);
return new ResponseEntity<String>("User deleted", HttpStatus.OK);
}else{
return new ResponseEntity<String>("User not deleted", HttpStatus.INTERNAL_SERVER_ERROR);
}
}```
Also in model packet habe ich folgende Klassen:
```public abstract class User {}
public abstract class Customer extends User {}
public class Owner extends Customer {}```
DAO Packet
```public interface UserDAO {
public void deleteUser(User user);
public User getUserByUsername(String username);
public Collection<? extends User> getUsers();
public void update(User user);
public void insertUser(User user);
}
public interface OwnerDAO extends UserDAO{
public Vehicle getVehicleByVehicleNumber(String vehicleNumber);
public void deleteVehicle(Vehicle vehicle);
public void insertVehicle(Vehicle vehicle);
}
@Repository
@Transactional
public abstract class UserDAOImpl implements UserDAO {
protected static List<User> users = new ArrayList<User>();
protected List<Vehicle> vehicles = new ArrayList<Vehicle>();
@Override
public abstract User getUserByUsername(String username);
public abstract List<User> getUsers();
}
public class OwnerDAOImpl extends UserDAOImpl implements OwnerDAO {
public OwnerDAOImpl() {
}
@Override
public void update(User user) {
this.sessionFactory.getCurrentSession().update(((Owner) user));
}
@Override
public User getUserByUsername(String username) {
return (Owner) sessionFactory.getCurrentSession().get(Owner.class,
username);
}
@Override
public void deleteUser(User user) {
sessionFactory.getCurrentSession().delete(user);
}
}
Controller Packet
import com.vb.dao.OwnerDAO;
import com.vb.dao.VehicleDAO;
import com.vb.model.Owner;
import com.vb.model.Vehicle;
import com.vb.model.User ;
@Controller
@RequestMapping("/owner")
public class OwnerController {
@Autowired
private OwnerDAO ownerDao;
@Autowired
private VehicleDAO vehicleDao;
public VehicleDAO getVehicleDao() {
return vehicleDao;
}
public void setVehicleDao(VehicleDAO vehicleDao) {
this.vehicleDao = vehicleDao;
}
public OwnerDAO getOwnerDao() {
return ownerDao;
}
public void setOwnerDao(OwnerDAO ownerDao) {
this.ownerDao = ownerDao;
}
public OwnerController() {
// TODO Auto-generated constructor stub
}
@RequestMapping(method=RequestMethod.POST, consumes="application/json")
public ResponseEntity<String> saveOwner(@RequestBody Owner owner) {
if (ownerDao.getUserByUsername(owner.getUsername()) == null) {
ownerDao.insertUser(owner);
return new ResponseEntity<String>("Owner saved", HttpStatus.OK);
} else {
return new ResponseEntity<String>("Owner exists",
HttpStatus.INTERNAL_SERVER_ERROR);
}
}
@RequestMapping(method = RequestMethod.DELETE, consumes ="application/json")
public ResponseEntity<String> deleteOwner(@RequestBody String username){
User owner = ownerDao.getUserByUsername(username);
return null;
}
}