What is the right way to do CRUD with nested domain objects in Struts 2?

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;
... //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
}

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) {
            //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
}

? ?

+3
1

  • , MatStatus Address . - ? "", "", , "" ""

  • . , . . Struts , , , , - -, . , - . .

, . , -, .

2 - , , / .

+1

Source: https://habr.com/ru/post/1786554/


All Articles