0
votes

I'm fairly new to Java and am working on a Spring Boot database application (MySQL) in which I am trying to create a method which will allow for a Client (Java object) to be deleted from my database using a Dao/CrudRepository and searching by the unique Id. I am receiving an error when running stating "error: incompatible types: int cannot be converted to Client"

I've compared to a similar program created in which I used the same syntax with no issue, so unsure why I am getting this error when processing.

My object is set up like so: @Entity public class Client {

@Id
@GeneratedValue
private int id;

@NotNull
@Size(min=1, message = "Must enter client name")
private String name;

@NotNull
@Size(min=1, message= "Must enter contact's name")
private String contact;

@NotNull
@Size(min=1, message="Must enter primary office location")
private String location;

@NotNull(message="Must enter contract start date")
private String startDate;

@NotNull(message="Must enter contract end date")
private String endDate;

@NotNull(message="Must enter employee count")
private Integer employeeCount;

private PhilanthropyInterest interest;

public Client(String name, String contact, String location, String startDate, String endDate, Integer employeeCount) {
    this.name = name;
    this.contact = contact;
    this.location = location;
    this.startDate = startDate;
    this.endDate = endDate;
    this.employeeCount = employeeCount;
}

public Client () { }

public int getId() { return id; }

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public String getContact() {
    return contact;
}

public void setContact(String contact) {
    this.contact = contact;
}

public String getLocation() {
    return location;
}

public void setLocation(String location) {
    this.location = location;
}

public String getStartDate() {
    return startDate;
}

public void setStartDate(String startDate) {
    this.startDate = startDate;
}

public String getEndDate() {
    return endDate;
}

public void setEndDate(String endDate) {
    this.endDate = endDate;
}

public Integer getEmployeeCount() {
    return employeeCount;
}

public void setEmployeeCount(Integer employeeCount) {
    this.employeeCount = employeeCount;
}

public PhilanthropyInterest getInterest() { return interest; }

public void setInterest(PhilanthropyInterest interest) { this.interest = interest; }

And my controller method in which I am receiving the error is:

@RequestMapping(value = "remove", method = RequestMethod.GET)
public String displayRemoveAddClientForm(Model model) {
    model.addAttribute("clients", clientDao.findAll());
    model.addAttribute("title", "Remove Client");
    return "clients/remove";
}

@RequestMapping(value = "remove", method = RequestMethod.POST)
public String processRemoveClientForm(@RequestParam int[] clientIds) {

    for (int clientId : clientIds) {
        clientDao.delete(clientId);
    }

    return "redirect:";
}

And my ClientDao class is:

@Repository
@Transactional
public interface ClientDao extends CrudRepository<Client, Integer> {

}

The error showing in IntelliJ states:

delete (org.launchcode.spcdb.models.Client) in CrudRepository cannot be applied to (int)

and when running, I am receiving:

error: incompatible types: int cannot be converted to Client clientDao.delete(clientId);

1
Post yout ClientDao class - LppEdd
the delete() method accept an object of type T, which in your case is a Client. Not a single int value - LppEdd

1 Answers

0
votes

here you can find the documentation for CRUDRepository : https://docs.spring.io/spring-data/commons/docs/current/api/org/springframework/data/repository/CrudRepository.html You will see that the method delete(T entity) method take an Object as argument. In your case, it is a Client.

As you are using

public interface ClientDao extends CrudRepository<Client, Integer>

the method delete(T entity) that you use is expecting an Client.

So try :

public String processRemoveClientForm(@RequestParam int[] clientIds) {
    List<Client> clientList = clientDAO.findByIds(clientIds);
    for (Client client : clientList) {
        clientDao.delete(client );
    }
}

this should work. Nerver forget that you are manipulating Object in this case.