I have two beans (POJOs) - a Customer and an address class defined like this:
public class Customer {
private String name = null;
private Address address = null;
public Customer() {
address = new Address();
}
public String getName() {
return name;
}
public void setName(name) {
this.name = name;
}
//additional setters/getters for various properties
}
public class Address {
private String street = null;
public String getStreet() {
return street;
}
public void setStreet(street) {
this.street = street;
}
//additional setters/getters for various properties
}
I'm trying to insert this to the database using like this:
public class CustomerDAO extends SimpleJdbcDaoSupport {
public int addOrganization(Customer customer) {
SimpleJdbcInsert insertCustomer = null;
BeanPropertySqlParameterSource params = null;
Number customerID = null;
insertTransaction = new SimpleJdbcInsert(getDataSource()).withTableName("customers")
.usingGeneratedKeyColumns("customerID");
params = new BeanPropertySqlParameterSource(customer);
customerID = insertTransaction.executeAndReturnKey(params);
return customerID.intValue();
}
}
The problem is I get an Invalid argument value: java.io.NotSerializableException
and it doesn't insert the Customer. I can remove the address from the database and it will insert the other customer data. Or, I can do something like this:
MapSqlParameterSource params = new MapSqlParameterSource();
params.addValue("name", customer.getName());
params.addValue("street", customer.getAddress().getStreet());
but that eliminates the ease of the BeanPropertySqlParameterSource
class and if I add or remove any properties, I have to add another line.
Is there an easy way to store the nested Address bean without having to manually add each value? How do I have to define the database and/or beans to make this happen?