我已经看到了一些示例,说明如何使用一个没有嵌套域对象(只是基元)的域对象来执行CRUD。问题是如何对具有对其他域对象的引用的域对象做同样的事情。给下面的例子:在Struts 2中使用嵌套域对象执行CRUD的正确方法是什么?
@Entity
public class Person implements Serializable {
@Id
private Long id;
private String name;
private Integer age;
private Address address;
private MaritalStatus maritalStatus;
... //getters/setters
}
@Entity
public class MaritalStatus implements Serializable {
@Id
private Long id;
private String description;
... //getters/setters
}
@Entity
public class Address implements Serializable {
@Id
private Long id;
private String street;
private String state;
private String zip;
... //getters/setters
}
比方说,我有一个创建一个窗体或更新的人,并要求以下输入:
名称:_ _
年龄:_ ____
街道:_ __
国家:_ ____
邮编:_ __ _
婚姻状况:(与相应的按键选择输入(ID的实体)/值)
那么,y ou创建或更新具有自己的标识的嵌套属性(保存在另一个表中)。
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) {
//find the person using the id.
Person p = personService.findById(person.getId());
//Update the reference to the selected martial status
//find the maritalstatus selected from the select box
MaritalStatus maritalStatus =
maritalStatusSerivce.findById(person.getMaritalStatus().getId());
//set the reference to the obtained person
p.setMaritalStatus(maritalStatus);
//find the address (in case it already exist)
if (person.getAddress().getId() != null) {
//find the address
Address address = addressService.findById(person.getAddress().getId());
//set the reference
p.setAddress(address);
}
//finally set the obtained reference to the action person property
this.person = p;
} else { //New person
//Find the address for the person
if (person.getAddress().getId() != null) {
//Only set the reference to the selected marital status
//find the maritalstatus selected from the select box
MaritalStatus maritalStatus =
maritalStatusSerivce.findById(person.getMaritalStatus().getId());
//set the reference to the person
person.setMaritalStatus(maritalStatus);
}
}
}
private Person person;
//getter/setters
}
是正确的方法:
我用的是准备方法和paramsPrepareParamsStack想这样?任何其他建议的方法?
感谢