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:objectin thymeleaf - Can we send
AddressandPersonentity together usingModelfrom 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 >
<h1 style="text-align: center">Add Person</h1>
<div >
<div >
<div >
<label for="exampleFormControlInput1" >Person name</label>
<input type="text" name="name" th:field="*{name}">
</div>
<div >
<label for="exampleFormControlInput1" >Person surname</label>
<input type="text" name="surname" th:field="*{surname}">
</div>
<div >
<label for="exampleFormControlInput1" >District</label>
<input type="text" name="district" th:field="*{district}">
</div>
<div >
<label for="exampleFormControlInput1" >City</label>
<input type="text" name="city" th:field="*{city}">
</div>
<input type="submit" value="Submit">
</div>
</div>
</div>
</form>
CodePudding user response:
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
CodePudding user response:
This is regarding boilerplate code. you could add @Data to the class coming from Lombok library.
If you are not using lombok add setter and getter
public String getDistrict() {
return district;
}
public void setDistrict(String district) {
this.district= district;
}
