当前位置: 首页 > 知识库问答 >
问题:

错误:不兼容的类型:int无法转换为Client-Java

唐麒
2023-03-14
@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; }
@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:";
}
@Repository
@Transactional
public interface ClientDao extends CrudRepository<Client, Integer> {

而当运行时,我收到的是:

错误:不兼容类型:int无法转换为客户端ClientDAO.Delete(clientId);

共有1个答案

邵锐
2023-03-14

您可以在这里找到CRUDRepository的文档:https://docs.spring.io/spring-data/commons/docs/current/api/org/springframework/data/repository/CRUDRepository.html。您将看到方法delete(T entity)方法将对象作为参数。在你的情况下,它是一个客户。

当您正在使用

public interface ClientDao extends CrudRepository<Client, Integer>

您使用的方法delete(T entity)需要一个客户端

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