【问题标题】:Bean property 'dipartmantId' is not readable or has an invalid getter method:Bean 属性“dipartmantId”不可读或具有无效的 getter 方法:
【发布时间】:2020-06-14 10:36:52
【问题描述】:

我在这里做错了什么,我遇到了以下错误

bean 类 [com.atlas.salesapplication.entity.Employee] 的无效属性“dipartmantId”:bean 属性“dipartmantId”不可读或具有无效的 getter 方法:getter 的返回类型是否与参数类型匹配二传手?

@Entity
@Table(name = "departmant")
public class Departmant {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    private Integer id;
    @Column(name = "name")
    private String name;

    @OneToMany(mappedBy = "departmantId")
    private List<Employee> employeeList;

    public List<Employee> getEmployeeList() {
        return employeeList;
    }

    public void setEmployeeList(List<Employee> employeeList) {
        this.employeeList = employeeList;
    }

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

    public Integer getId(){ return id; }

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

    public String getName(){ return name; }

    public Departmant(){}

    public Departmant(Integer id, String name){
        this.id = id;
        this.name = name;
    }

    @Override
    public String toString(){
        return name;
    }
}

Employee类如下,

@Entity
@Table(name = "employee")
public class Employee {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    private Integer id;
    @Column(name = "firstname")
    @NotEmpty(message = "")
    @Size(min=3, message = "First name should ane minimum 3 letters.")
    private String firstName;
    @Column(name = "lastname")
    @NotEmpty(message = "")
    @Size(min=3, message = "Last name should ane minimum 3 letters.")
    private String lastName;
    @Column(name = "dob")
    @DateTimeFormat(pattern = "yyyy-MM-dd")
    @NotNull(message = "Please enter a Date of Birth!")
    private Date dob;
    @Column(name = "nic")
    @NotEmpty(message = "")
    @Pattern(regexp = "([0-9]{9}[V|v|X|x])|([0-9]{12})",message = "Please enter valid email!")
    private String nic;
    @Column(name = "moblie")
    @NotEmpty(message = "")
    @Pattern(regexp = "((07)(0|1|2|5|6|7|8)[0-9]{7})",message = "Please enter valid mobile number!")
    private String mobile;
    @Column(name = "home")
    @NotEmpty(message = "")
    @Pattern(regexp = "([0-9]{10})",message = "Please enter valid phone number!")
    private String home;
    @Column(name = "addressline1")
    @NotEmpty(message = "")
    private String addressLine1;
    @Column(name = "addressline2")
    private String addressLine2;
    @Column(name = "city")
    @NotEmpty(message = "")
    private String city;
    @Column(name = "image")
    private byte [] image;
    @Column(name = "status")
    private boolean status;

    @ManyToOne
    @JoinColumn(name ="gender_id")
    private  Gender genderId;

    @ManyToOne
    @JoinColumn(name = "designation_id")
    private Designation designationId;

    @ManyToOne
    @JoinColumn(name = "departmant_id")
    private Departmant departmantId;

    public Departmant getDepartmantId() {
        return departmantId;
    }

    public void setDepartmantId(Departmant departmantId) {
        this.departmantId = departmantId;
    }

    public Designation getDesignationId() {
        return designationId;
    }

    public void setDesignationId(Designation designationId) {
        this.designationId = designationId;
    }

    public Gender getGenderId() {
        return genderId;
    }

    public void setGenderId(Gender genderId) {
        this.genderId = genderId;
    }

    public boolean isStatus() {
        return status;
    }

    public void setStatus(boolean status) {
        this.status = status;
    }

    public Employee() {
    }

    public Integer getId() {
        return id;
    }

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

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public Date getDob() {
        return dob;
    }

    public void setDob(Date dob) {
        this.dob = dob;
    }

    public String getNic() {
        return nic;
    }

    public void setNic(String nic) {
        this.nic = nic;
    }

    public String getMobile() {
        return mobile;
    }

    public void setMobile(String mobile) {
        this.mobile = mobile;
    }

    public String getHome() {
        return home;
    }

    public void setHome(String home) {
        this.home = home;
    }

    public String getAddressLine1() {
        return addressLine1;
    }

    public void setAddressLine1(String addressLine1) {
        this.addressLine1 = addressLine1;
    }

    public String getAddressLine2() {
        return addressLine2;
    }

    public void setAddressLine2(String addressLine2) {
        this.addressLine2 = addressLine2;
    }

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }

    public byte[] getImage() {
        return image;
    }

    public void setImage(byte[] image) {
        this.image = image;
    }

    public Employee(Integer id, String firstName, String lastName, String nic, String mobile, String home, Date dob, String addressLine1, String addressLine2, String city, byte[] image){
        this.id = id;
        this.firstName = firstName;
        this.lastName = lastName;
        this.nic = nic;
        this.mobile = mobile;
        this.home = home;
        this.dob = dob;
        this.addressLine1 = addressLine1;
        this.addressLine2 = addressLine2;
        this.city = city;
        this.image = image;
    }


    @Override
    public String toString(){
        return firstName+ " "+lastName;
    }
}

【问题讨论】:

  • 您能否展示 Employee 类并提供更多有关错误发生方式和时间的上下文?
  • @codependent 我加了先生
  • 是部门,不是部门

标签: java spring spring-boot spring-mvc


【解决方案1】:

根据你提供的错误:

Invalid property 'dipartmantId' of bean class [com.atlas.salesapplication.entity.Employee]: Bean property 'dipartmantId' is not readable or has an invalid getter method: Does the return type of the getter match the parameter type of the setter?

正在尝试访问来自 Employee 类的 dipartmantId 属性。由于 Employee 类中的属性实际上称为departmantId,因此似乎有一个错字。

在您的代码中搜索dipartmantId 的用法并将其更改为departmantId

【讨论】:

    【解决方案2】:

    您的代码中有很多不同的拼写错误。我会先纠正所有这些,我敢打赌你的错误会消失(还有其他一些错误):

    • 将departmant 的所有变体重命名为department
    • 将moblie 重命名为mobile
    • 在您的代码中找到dipartmantId 并将其重命名为departmentId
    • 考虑将包含电子邮件的列命名为email 而不是noc

    【讨论】:

      猜你喜欢
      • 2015-03-03
      • 1970-01-01
      • 1970-01-01
      • 2021-06-16
      • 1970-01-01
      • 1970-01-01
      • 2020-11-27
      • 1970-01-01
      • 2019-12-13
      相关资源
      最近更新 更多