You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user-java@ibatis.apache.org by Zarar Siddiqi <za...@utoronto.ca> on 2005/06/14 15:43:14 UTC

Multiple Objects as parameters

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.


Re: Multiple Objects as parameters

Posted by Zarar Siddiqi <za...@utoronto.ca>.
Hi Larry,

I like your first solution and will adopt it.  Thank you.

As for storing the userId inside the Address object, your point is 
well-taken
and will work in this case since it is a m:1 relationship.  However, in a 
more
complex scenario say Student and Course, it would be incorrect for the
Course object to store a List of  studentIds.  I've extended this paradigm
and applied it (perhaps incorrectly) to User/Address.  Maybe I need to
re-think my m:1 relationships so that programming the DAOs is easier.

Thanks for your help.

Zarar




----- Original Message ----- 
From: "Larry Meadors" <la...@gmail.com>
To: <us...@ibatis.apache.org>
Sent: Tuesday, June 14, 2005 10:22 AM
Subject: Re: Multiple Objects as parameters


> 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
> (unneccesarily).
>
> Larry
>
>
> On 6/14/05, Zarar Siddiqi <za...@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.
>>
>>
> 



Re: Multiple Objects as parameters

Posted by Larry Meadors <la...@gmail.com>.
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
(unneccesarily).

Larry


On 6/14/05, Zarar Siddiqi <za...@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.
> 
>