ibatis-user-java mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Larry Meadors <larry.mead...@gmail.com>
Subject Re: Multiple Objects as parameters
Date Tue, 14 Jun 2005 14:22:09 GMT
you could do a couple of things:

1) Put both the use and address into a map:
Map params = new HashMap();
params.put("user", user);
params.put("address", address);

2) Create a bean for them:
class UserAddress{
  private User user;
  private Address address;
  public void setUser(User u){this.user = u;}
  public void setAddress(Address a){this.address = a;}
  // etc..

In either case, you can refer to the parameter object the same way in
your sql map: userAddress.user.userId

Now, on a related note...if you have a userId in the database, why not
have it in the related bean? I am not sure I understand why you say
"It doesn't make sense for the Address object to store a UserId."
Because you are storing a userId...just in the database...and
disconnecting it artificially in your Java code. From what I can see,
that adds no value, and increases the complexity of the system


On 6/14/05, Zarar Siddiqi <zarar.siddiqi@utoronto.ca> wrote:
> 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.

View raw message