gpt4 book ai didi

mysql - 如何修复 "Coult not find Id property. Be sure the accessor method name matches the veriable name"错误

转载 作者:行者123 更新时间:2023-11-29 23:24:58 24 4
gpt4 key购买 nike

当我按下“完成”按钮时,我创建了“从实体类新建 JPA Controller 类”,然后收到以下错误。

enter image description here enter image description here

Users.java

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package com.mycompany.mavenproject1.exceptions.exceptions.exceptions;

import java.io.Serializable;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import javax.xml.bind.annotation.XmlRootElement;

/**
*
* @author OZGUR-PC
*/
@Entity
@Table(name = "users")
@XmlRootElement
@NamedQueries({
@NamedQuery(name = "Users.findAll", query = "SELECT u FROM Users u"),
@NamedQuery(name = "Users.findById", query = "SELECT u FROM Users u WHERE u.id = :id"),
@NamedQuery(name = "Users.findByUserName", query = "SELECT u FROM Users u WHERE u.userName = :userName")})
public class Users implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Basic(optional = false)
@Column(name = "id")
private Integer id;
@Basic(optional = false)
@NotNull
@Size(min = 1, max = 50)
@Column(name = "user_name")
private String userName;

public Users() {
}

public Users(Integer id) {
this.id = id;
}

public Users(Integer id, String userName) {
this.id = id;
this.userName = userName;
}

public Integer getId() {
return id;
}

public void setId(Integer id) {
this.id = id;
}

public String getUserName() {
return userName;
}

public void setUserName(String userName) {
this.userName = userName;
}

@Override
public int hashCode() {
int hash = 0;
hash += (id != null ? id.hashCode() : 0);
return hash;
}

@Override
public boolean equals(Object object) {
// TODO: Warning - this method won't work in the case the id fields are not set
if (!(object instanceof Users)) {
return false;
}
Users other = (Users) object;
if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
return false;
}
return true;
}

@Override
public String toString() {
return "com.mycompany.mavenproject1.exceptions.exceptions.exceptions.Users[ id=" + id + " ]";
}

}

我需要关于如何解决这个问题的想法。预先感谢您的帮助。

最佳答案

确保您已为您的实体声明了标识符。例如,假设有一个名为 Student 的实体,其标识符为 studentId 和属性 name 那么该实体应该是这样的,它应该有 setter以及字段的 getter 方法:

@Entity
public class Student {
@Id
@GeneratedValue
private int studentId;
private String name;

public String getName() {
return name;
}

public int getStudentId() {
return studentId;
}

public void setStudentId(int studentId) {
this.studentId = studentId;
}

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

关于mysql - 如何修复 "Coult not find Id property. Be sure the accessor method name matches the veriable name"错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27043511/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com