Hello,

I have seen examples that show how to do CRUD with one domain object
without nested domain objects (just primitives). Giving 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 which creates or update a Person and ask for the
following inputs:
Name: ________
Age: ___________
Street: _________
State: ___________
Zip: ____________
Marital Status: (a select input with the corresponding key(id of the
entity)/value)

So how do you create or update having nested properties which has their
own identity too (persisted in another table).

I was thinking something like this using the prepare method and
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
}

Is that the correct way? Any other suggested approach?

Thanks

Alfredo Osorio

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscr...@struts.apache.org
For additional commands, e-mail: user-h...@struts.apache.org

Reply via email to