I saw examples that show how to do CRUD with one domain object without nested domain objects (just primitives). The problem is how to do the same with a domain object that has links to other domain objects. We give the following example:
@Entity
public class Person implements Serializable {
@Id
private Long id;
private String name;
private Integer age;
private Address address;
private MaritalStatus maritalStatus;
...
}
@Entity
public class MaritalStatus implements Serializable {
@Id
private Long id;
private String description;
...
}
@Entity
public class Address implements Serializable {
@Id
private Long id;
private String street;
private String state;
private String zip;
...
}
Let's say I have a form that creates or updates a Person and requests the following inputs:
Name: __
Age: _____
Street: ___
Status: _____
Zip / Postal Code: ____
Family status: (select the entry with the corresponding key (object id) / value)
, , ( ).
, paramsPrepareParamsStack:
public class PersonAction extends ActionSupport {
public String save() {
personService.save(person);
return SUCCESS;
}
public String update() {
personService.update(person);
return SUCCESS;
}
public void prepare() {
if (person.getId() != null) {
Person p = personService.findById(person.getId());
MaritalStatus maritalStatus =
maritalStatusSerivce.findById(person.getMaritalStatus().getId());
p.setMaritalStatus(maritalStatus);
if (person.getAddress().getId() != null) {
Address address = addressService.findById(person.getAddress().getId());
p.setAddress(address);
}
this.person = p;
} else {
if (person.getAddress().getId() != null) {
MaritalStatus maritalStatus =
maritalStatusSerivce.findById(person.getMaritalStatus().getId());
person.setMaritalStatus(maritalStatus);
}
}
}
private Person person;
}
? ?