Multiple Objects as parameters
Hi,
I've encountered a situation which I am sure most developers
have faced and resolved in their own way when using iBatis.
Consider the following scenario. We have two tables:
USER(UserId,Name,Age,Occupation)
ADDRESS(AddressId,UserId,Street,City,ZipCode,State,Country);
Pretty simple case. A user has multiple addresses.
It doesn't make sense for the Address object to store a UserId.
So, it looks like:
public class Address {
private String addressId, street, city, zipCode, state, country;
// more methods here
}
Now, when adding an Address, I also have to store the userId which I
have access to in my Java code but have no clear means of passing it
in via the SqlMapClient. So, the work around for me is:
// Get a new address
Address addr = RequestObjectFactory.getAddress(request);
// Copy fields of addr into a map
Map params = PropertyUtils.describe(addr);
// Add userId which is not present in addr
params.put("userId", new Integer(userId));
// Do insert
sqlMapClient.insert("addAddress", params);
The problem with the above code is that it relies on PropertyUtils
which I am assuming does some serious reflection to get the fields
of addr. This I want to avoid.
Is there a mechanism (or a better workaround) that allows for more
than one parameter to be passed into a query, in this case addr and userId.