I have this Person entity and it has a child field which is the Parent Person and join to itself via parent_id. When I tried to hit Update, why is the person.getParentPerson() is always null?
@javax.persistence.Table(name = "person")
@Entity
public class Person implements Serializable {
// Fields
@Column(name = "id", nullable = false, insertable = true, updatable = true, length = 20, precision = 0, unique =
true)
@Id
private Long id;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "parent_id")
private ParentPerson parentPerson;
@Column(name = "name", nullable = true, insertable = true, updatable = true, length = 10)
private String name;
//some other fields, setters and getters
....
}
@Controller
@RequestMapping("/person")
public class PersonController {
@RequestMapping("/add.htm")
private ModelAndView personEdit() {
Person person = new Person();
Person parentPerson = new ParentPerson();
if (checkIsParent()) {
parentPerson.setId(-1);
}
person.setParentPerson(parentPerson);
mav.addObject("person", person)
return mav;
}
@RequestMapping("/update.htm")
@ModelAttribute("person") @Valid Person person
private ModelAndView personUpdate() {
//person.getParentPerson() is null
}
}
HTML snippet:
<form id="uForm" action="update.htm" method="post" accept-charset="UTF-8" autocomplete="off">
<@global.spring.formInput path="person.id" attributes='size="40"'/>
<@global.spring.formInput path="person.name" attributes='size="40"'/>
</form>