为什么 bean class 无效 属性 'district'

Why got Invalid property 'district' of bean class

运行 spring 引导项目时,主页出现错误 Invalid property 'district' of bean class。 我知道为什么会出现此错误,因为 district 是子实体的 属性,我可以从控制器中的 Home() 方法传递父实体。我可以在 Home() 方法中传递模型中的 Person 实体。但是 districtcity 属性 来自 Address 实体 我正在使用 OneToOne 关系映射。

我的问题如下:

堆栈跟踪:

Caused by: org.springframework.beans.NotReadablePropertyException: Invalid property 'district' of bean class [com.rest.RestApiPojo.Entity.Person]: Bean property 'district' is not readable or has an invalid getter method: Does the return type of the getter match the parameter type of the setter?

下面是我的代码:

实体

@Entity
@Table(name = "person_master")
public class Person {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long p_id;

    private String name;

    private String surname;

    @OneToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
    private Address address;

    // getter setter
}

@Entity
@Table(name = "address_master")
public class Address {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long a_id;

    private String district;
    private String city;

    @OneToOne(cascade = CascadeType.ALL, mappedBy = "address")
    @JoinColumn(name = "p_id")
    private Person person;

    // getter setter
}

服务

@Override
public Person addPersonAddress(Person person) {
    return personRepo.save(person);
}

控制器

@RequestMapping(value = "/", method = RequestMethod.GET)
public String Home(Model mdl)
{
    mdl.addAttribute("persons", new Person());
    return "register";
}
    
@RequestMapping(value = "/personaddress", method = RequestMethod.POST)
public String addPersonAddress(Model mdl, @ModelAttribute("person") Person person, HttpServletRequest req)
{
    Address address = person.getAddress();  // get reference of person from parent table and store in child table
        
    address.setDistrict(req.getParameter("district"));
    address.setCity(req.getParameter("city"));
        
    address.setPerson(person);
    pojoService.addPersonAddress(person);
    return "listofperson";
}

百里香叶

<form th:action="@{/personaddress}" th:object="${persons}" method="post">
        <div class="container">
        <h1 style="text-align: center">Add Person</h1>
          <div class="row">
            <div class="col-sm-12">
                <div class="mb-3">
                  <label for="exampleFormControlInput1" class="form-label">Person name</label>
                  <input type="text" class="form-control" name="name" th:field="*{name}">
                </div>
                <div class="mb-3">
                  <label for="exampleFormControlInput1" class="form-label">Person surname</label>
                  <input type="text" class="form-control" name="surname" th:field="*{surname}">
                </div>
                <div class="mb-3">
                  <label for="exampleFormControlInput1" class="form-label">District</label>
                  <input type="text" class="form-control" name="district" th:field="*{district}">
                </div>
                <div class="mb-3">
                  <label for="exampleFormControlInput1" class="form-label">City</label>
                  <input type="text" class="form-control" name="city" th:field="*{city}">
                </div>
                
                <input class="btn btn-primary" type="submit" value="Submit">
            </div>
          </div>
        </div>
    </form>

这是关于样板代码的。您可以将@Data 添加到来自 Lombok 库的 class。

如果您不使用 lombok 添加 setter 和 getter

public String getDistrict() {
   return district;
}

public void setDistrict(String district) {
   this.district= district;
}

不需要两个实体,因为它们已经映射到人身上 class 只需在 thymeleaf 中写入 'address.district' 和 'address.city',你就会得到它