struts-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Alfredo Manuel Osorio Martinez" <alfredo.oso...@afirme.com>
Subject RE: Create/Update with nested domain objects
Date Thu, 20 Jan 2011 15:04:41 GMT
Any thoughts on this? Please...

Alfredo Osorio

-----Mensaje original-----
De: Alfredo Manuel Osorio Martinez 
Enviado el: Tuesday, January 18, 2011 10:15 AM
Para: Struts Users Mailing List
Asunto: Create/Update with nested domain objects

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-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Mime
View raw message