Search code examples
javaspring-bootspring-mvcthymeleaf

Why got Invalid property 'district' of bean class


While run spring boot project, got error in home page Invalid property 'district' of bean class. I know why this error is coming because district is property of child entity and i can pass parent entity from Home()method in controller. I could pass Person entity in model in Home() method. but district and city property is from Address entity I am working with OneToOne relationship mapping.

My question are below:

  • Can we get two entity together in th:object in thymeleaf
  • Can we send Address and Person entity together using Model from controller to view

Stacktrace:

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?

Here down is my code:

Entity

@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
}

service

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

controller

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

Thymeleaf

<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>

Solution

  • Two entities are not required as they have already been mapped in the person class just write 'address.district' and 'address.city' in the thymeleaf, you will get it