Here is an example from the official documentation (You need to scroll down a little):
class Person {
private final @Id Long id;
private final String firstname, lastname;
private final LocalDate birthday;
private final int age;
private String comment;
private @AccessType(Type.PROPERTY) String remarks;
static Person of(String firstname, String lastname, LocalDate birthday) {
return new Person(null, firstname, lastname, birthday,
Period.between(birthday, LocalDate.now()).getYears());
}
Person(Long id, String firstname, String lastname, LocalDate birthday, int age) {
this.id = id;
this.firstname = firstname;
this.lastname = lastname;
this.birthday = birthday;
this.age = age;
}
Person withId(Long id) {
return new Person(id, this.firstname, this.lastname, this.birthday);
}
void setRemarks(String remarks) {
this.remarks = remarks;
}
}
I have a few questions about this.
Person withId(Long id)
Does the referenced construction function not exist in the example?Person withId(Long id)
There is a description in the official documentation :"The same pattern is usually applied for other properties that are store managed but might have to be changed for persistence operations". Can I understand it this way: After successfully saving the instance,WithOutIdPerson
can be used for other field changes and saved again, andSavedPerson
can be used for other upper level operations?Why is the last step of the factory
of ()
called in the example??
Thank you~