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 Eric Sheffer <es...@lycos.com> on 2005/08/17 17:58:46 UTC

Lists of like objects as parameters to SqlMaps

First of all, thanks for developing iBATIS!  It's very slick!

Perhaps I'm missing something, but I've been unable to find any
information about this question.  I've searched through the 
documentation and posts to the mailing list and I'm surprised this 
question hasn't been asked yet: is it possible to pass as a parameter 
to a SqlMap a list of like objects that the framework would then act on?

For example, I have an ArrayList of objects of type Widget.  I'd 
like to insert into the database a row for each object in the 
collection. It seems like the only way to do that is to write code 
to iterate over the collection and call a method like "insertWidget" 
on an WidgetSqlMapDao. 

A logical extension to this (for me, at least ;-) is to pass the 
collection directly to iBATIS and let it do the iterating. In the 
following example, if the Widget SqlMap is defined as:


<sqlmap id="Widget">

  <typeAlias alias="widget" type="com.yoyodyne.domain.Widget"/>
  <typeAlias alias="arrayList" type="java.util.ArrayList"/>

  <parameterList id="widgetList" class="widget" type="arraylist"/>

  <insert id="insertWidgets" parameterList="widgetList">
     insert into WIDGET (WIDGET_ID, WIDGET_NAME, CATEGORY, PRICE)
     values (WIDGET_SEQ.NEXTVAL, #widgetName#, #category#, #price#)
  </insert>

</sqlmap>


the framework could handle the iterating knowing that its being
passed a list containing a collection of objects of type Widget.
The new <parameterList/> tag would define a list of a specific
type that contains instances of the specified class.  The parameterList
attribute of the insert tag would tell the framework about the 
list mapping and indicate that it is to iterate over the collection.

I suppose the use of type arrays (i.e. Widget[]) could be considered
as well.

Am I out in the weeds here? Can iBATIS do this now?  Is there any 
benefit to something like this?  Drawbacks?

Anyway, again thanks for creating such a useful tool.

Eric



-- 
_______________________________________________

Search for businesses by name, location, or phone number.  -Lycos Yellow Pages

http://r.lycos.com/r/yp_emailfooter/http://yellowpages.lycos.com/default.asp?SRC=lycos10


Re: Lists of like objects as parameters to SqlMaps

Posted by Koka <22...@gmail.com>.
It will be indeed very handy to have list
insert/update/delete/statement methods.

And it even seems quite natural to have
queryForObject - queryForList
      insert            -  insertList(String statementName, List list) etc.

Koka

Re: Lists of like objects as parameters to SqlMaps

Posted by Prashanth Sukumaran <pr...@yahoo.com>.
Hi Eric,

Sometimes it is not good to advice people when you don't know it fully yourself.  But this is how
i would do.

In normal JDBC i would achieve this by batchInsert.  Similar is the case with iBatis.

When executing in batch mode i do this.

try {
	startTransaction();

	startBatch();
	for (int i = 0; i < dealerFrLines.length; i++) {
		param.put("dealerFrLines", new Integer(dealerFrLines[i]));
		insert("insertShopDFL", param);
	}
	executeBatch();
	
	commitTransaction();

} finally {
	endTransaction();
}


Rgds

Prashanth.

--- Eric Sheffer <es...@lycos.com> wrote:

> First of all, thanks for developing iBATIS!  It's very slick!
> 
> Perhaps I'm missing something, but I've been unable to find any
> information about this question.  I've searched through the 
> documentation and posts to the mailing list and I'm surprised this 
> question hasn't been asked yet: is it possible to pass as a parameter 
> to a SqlMap a list of like objects that the framework would then act on?
> 
> For example, I have an ArrayList of objects of type Widget.  I'd 
> like to insert into the database a row for each object in the 
> collection. It seems like the only way to do that is to write code 
> to iterate over the collection and call a method like "insertWidget" 
> on an WidgetSqlMapDao. 
> 
> A logical extension to this (for me, at least ;-) is to pass the 
> collection directly to iBATIS and let it do the iterating. In the 
> following example, if the Widget SqlMap is defined as:
> 
> 
> <sqlmap id="Widget">
> 
>   <typeAlias alias="widget" type="com.yoyodyne.domain.Widget"/>
>   <typeAlias alias="arrayList" type="java.util.ArrayList"/>
> 
>   <parameterList id="widgetList" class="widget" type="arraylist"/>
> 
>   <insert id="insertWidgets" parameterList="widgetList">
>      insert into WIDGET (WIDGET_ID, WIDGET_NAME, CATEGORY, PRICE)
>      values (WIDGET_SEQ.NEXTVAL, #widgetName#, #category#, #price#)
>   </insert>
> 
> </sqlmap>
> 
> 
> the framework could handle the iterating knowing that its being
> passed a list containing a collection of objects of type Widget.
> The new <parameterList/> tag would define a list of a specific
> type that contains instances of the specified class.  The parameterList
> attribute of the insert tag would tell the framework about the 
> list mapping and indicate that it is to iterate over the collection.
> 
> I suppose the use of type arrays (i.e. Widget[]) could be considered
> as well.
> 
> Am I out in the weeds here? Can iBATIS do this now?  Is there any 
> benefit to something like this?  Drawbacks?
> 
> Anyway, again thanks for creating such a useful tool.
> 
> Eric
> 
> 
> 
> -- 
> _______________________________________________
> 
> Search for businesses by name, location, or phone number.  -Lycos Yellow Pages
> 
> http://r.lycos.com/r/yp_emailfooter/http://yellowpages.lycos.com/default.asp?SRC=lycos10
> 
> 


__________________________________________________
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 
http://mail.yahoo.com