【问题标题】:error: incompatible types: int cannot be converted to Client - Java错误:不兼容的类型:int 无法转换为客户端 - Java
【发布时间】:2019-07-10 13:41:25
【问题描述】:

我对 Java 还很陌生,正在开发 Spring Boot 数据库应用程序 (MySQL),我正在尝试创建一个方法,该方法允许使用 Dao 从我的数据库中删除客户端(Java 对象) /CrudRepository 并按唯一 ID 搜索。运行时收到错误提示“错误:不兼容的类型:int 无法转换为客户端”

我与创建的类似程序进行了比较,在该程序中我使用相同的语法没有问题,所以不确定为什么我在处理时收到此错误。

我的对象是这样设置的: @实体 公共类客户{

@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:";
}

我的 ClientDao 类是:

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

}

IntelliJ 中显示的错误状态:

删除 (org.launchcode.spcdb.models.Client) 在 CrudRepository 中无法应用 到 (整数)

运行时,我收到:

错误:不兼容的类型:int 无法转换为 Client clientDao.delete(clientId);

【问题讨论】:

  • 发布你的ClientDao类
  • delete() 方法接受 T 类型的对象,在您的情况下是客户端。不是单个 int 值

标签: java mysql spring-mvc intellij-idea dao


【解决方案1】:

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

当你使用时

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 );
    }
}

这应该可以。在这种情况下,千万不要忘记您正在操作 Object。

【讨论】:

    猜你喜欢
    • 2016-09-05
    • 2023-01-18
    • 2017-10-03
    • 2018-05-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-27
    • 2019-08-22
    相关资源
    最近更新 更多