I have a data model that consists of a master - detail relationship where one Client can be related to many Address entities. I have generated the domain objects using Hibernate Tools and I get the following ( simplified ) classes.
Client.java
@Entity
@Table(name="Client")
public class Client implements java.io.Serializable {
private Set<Address> addresses = new HashSet<Address>(0);
@OneToMany(fetch=FetchType.LAZY, mappedBy="client")
public Set<Address> getAddresses() {
return this.addresses;
}
public void setAddresses(Set<Address> addresses) {
this.addresses = addresses;
}
}
Address.java
@Entity
@Table(name="Address")
public class Address implements java.io.Serializable {
private AddressId id;
private AddressType addressType;
private Client client;
private String addressLine1;
@AttributeOverrides( {
@AttributeOverride(name="clientId", column=@Column(name="ClientId", nullable=false) ),
@AttributeOverride(name="addressTypeId", column=@Column(name="AddressTypeId", nullable=false) ) } )
public AddressId getId() {
return this.id;
}
public void setId(AddressId id) {
this.id = id;
}
@ManyToOne(fetch=FetchType.LAZY)
@JoinColumn(name="ClientId", nullable=false, insertable=false, updatable=false)
public Client getClient() {
return this.client;
}
public void setClient(Client client) {
this.client = client;
}
}
I'm wanting to write a form that allows the user to add a client record and an address record at the same time, so, in my controller, I create a Client instance and put it into the ModelMap. I can happily refer to the fields on the Client object in my jsp, but have no idea how to refer to the fields in the Address object.
Code like <form:input type="text" path="addresses[0].addressLine1" />
results in the following exception
org.springframework.beans.InvalidPropertyException: Invalid property 'addresses[0]' of bean class [com.greenock.saltcam.hibernate.domain.Client]: Illegal attempt to get property 'addresses' threw exception; nested exception is org.springframework.beans.InvalidPropertyException: Invalid property 'addresses[0]' of bean class [com.greenock.saltcam.hibernate.domain.Client]: Cannot get element with index 0 from Set of size 0, accessed using property path 'addresses[0]'
This doesn't strike me as unreasonable ( the set is empty ), but does that mean that I need to create an empty Address object in my controller and push it in to the Client object before passing it out to the jsp? What if I want to allow the user toadd an arbitrary number of addresses?
I would suggest creating a separate form-backing object:
import org.springframework.util.AutoPopulatingList;
public class ClientBackingBean {
private Client client = new Client();
private List<Address> addresses = new AutoPopulatingList<Address>(Address.class);
...
<getters and setters>
}
Then put one of those in your ModelMap
. You will just have to transfer the data from the backing bean to your actual model objects in the controller method that handles the form post. The nice thing about Spring's AutoPopulatingList
is that it will create entries as needed, so you could have javascript/jQuery in your JSP that creates addresses on the fly.