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 Collin Peters <ca...@gmail.com> on 2007/03/02 21:07:00 UTC

Using an array instead of a List

This is the same question as this post, which never got fully
answered: http://www.mail-archive.com/user-java@ibatis.apache.org/msg06454.html

I have a POJO which has an array of another POJO in it (N+1 style from
the iBatis guide).  I have gotten this to work with a java.util.List,
but I need it to also work with a straight Java array.  The reason for
doing this is simply because I want to use the same POJO with some web
services.  I am using Axis2 for web services which does not support
(yet) using a List, you must use an old-school array of objects.

So the question is simple.  Can iBatis support an array of POJO
objects instead of using a List of POJO objects?  I know using a List
would be better, and I wish I could, but I cannot at the moment.

Sample POJOs:

public class ProgramVO {

	private Integer programID;
	private ProgramActivityVO[] activities;  //Using an array instead of a List

	public Integer getProgramID() {
		return programID;
	}
	public void setProgramID(Integer programID) {
		this.programID = programID;
	}

	public ProgramActivityVO[] getActivities() {
		return activities;
	}
	public void setActivities(ProgramActivityVO[] activities) {
		this.activities = activities;
	}

}


public class ProgramActivityVO {

	private Integer programActivityID;
	private Integer activityID;

	public Integer getProgramActivityID() {
		return programActivityID;
	}
	public void setProgramActivityID(Integer programActivityID) {
		this.programActivityID = programActivityID;
	}
	
	public Integer getActivityID() {
		return activityID;
	}
	public void setActivityID(Integer activityID) {
		this.activityID = activityID;
	}
}

SQL Map:

	<resultMap id="programResult" class="Program" groupBy="programID">
		<result property="programID"/>
		<result property="activities" resultMap="program.programActivityResult"/>
	</resultMap>

	<resultMap id="programActivityResult" class="ProgramActivity">
		<result property="programActivityID" column="programActivityID" />
		<result property="activityID" column="activityID" />
	</resultMap>


Regards,
Collin Peters

Re: Using an array instead of a List

Posted by Koka Kiknadze <22...@gmail.com>.
>
>   I
> realize that Lists and Collections are better but an array is still a
> common data structure.
>
> Collin
>

I guess the problem here is that when querying the database you never know
how many rows will be retruned (unless you run separate count query
beforehand), so most natural way is to populate a List with returned rows.
Of course one could add ad hoc convertion to the Array, but seems this is
left for us if we need arrays returned...

Re: Using an array instead of a List

Posted by Ted Schrader <te...@gmail.com>.
Hi Collin,

I must admit I have not worked with web services, so having a
List-backed array may not be a good fit.  This pattern seems to work
pretty well for me in sending objects to JSPs for rendering, though.

In regards to iBATIS not supporting arrays, in a way you are right
because you will not get an array directly out of iBATIS.  You can
always queryForList() and then create an array from the returned List
with some middleman code.

However, the use of the <iterate> tag in dynamic mapped statements can
be fed an array parameter, so iBATIS does support arrays in this
regard.

Ted


On 02/03/07, Collin Peters <ca...@gmail.com> wrote:
> Hi Ted,
>
> I believe since the List still exists in the VO, Axis will complain.
> You would essentially end up having two versions of the VO which would
> wreck havoc on having proper code using the services (i.e. you
> essentially would have to have two versions of everything)
>
> So does this mean that iBatis truly doesn't support Java arrays?  I
> would find this to be pretty interesting if that is the case.  I
> realize that Lists and Collections are better but an array is still a
> common data structure.
>
> Collin
>
> On 3/2/07, Ted Schrader <te...@gmail.com> wrote:
> > Hi Collin,
> >
> > How about a derived getter for use by the web services?  Here's what I mean:
> >
> > public class ProgramVO {
> >
> >        // programID is in here somewhere.
> >
> >        // List of ProgramActivityVOs
> >        private List activities;
> >
> >        public List getActivities() {
> >                return activities;
> >        }
> >        public void setActivities(List activities) {
> >                this.activities = activities;
> >        }
> >
> >       /**
> >        * Derived getter. See getActivites().
> >        *  Note: I did not compile this; it's just for illustration.
> >        *  Probably could be more efficient, too.
> >        */
> >       public ProgramActivityVO[] getActivitiesAsArray() {
> >            final ProgramActivityVO[] result =
> >                   new ProgramActivityVO[getActivites().size()];
> >
> >            for(int i = 0; i < getActivites().size(); ++i) {
> >                 result[i] = (ProgramActivityVO)getActivites().get(i);
> >            }
> >
> >            return result;
> >       }
> > }
> >
> >
> > This way, iBATIS can give you the List, and you can give Axis the
> > array it needs by calling getActivitesAsArray() instead of
> > getActivities().
> >
> > Ted
> >
> >
> >
> > On 02/03/07, Collin Peters <ca...@gmail.com> wrote:
> > > I'm not sure if I follow.  After looking over the <iterate> tag, it
> > > looks like it is only for adding dynamic SQL *into* a select
> > > statement.  I am looking for a way to get data *out of* a select
> > > statement and into a Java array.
> > >
> > > Do you have any examples or links of what you mean?
> > >
> > > On 3/2/07, Poitras Christian <Ch...@ircm.qc.ca> wrote:
> > > > From what I know, arrays can be used with getters, by not setters.
> > > > For instance, the <iterate> tag should work with arrays and Lists, but
> > > > not resultMap.
> > > >
> > > > Christian
> > > >
> > > > -----Original Message-----
> > > > From: Collin Peters [mailto:cadiolis@gmail.com]
> > > > Sent: Friday, 02 March 2007 15:07
> > > > To: user-java@ibatis.apache.org
> > > > Subject: Using an array instead of a List
> > > >
> > > > This is the same question as this post, which never got fully
> > > > answered:
> > > > http://www.mail-archive.com/user-java@ibatis.apache.org/msg06454.html
> > > >
> > > > I have a POJO which has an array of another POJO in it (N+1 style from
> > > > the iBatis guide).  I have gotten this to work with a java.util.List,
> > > > but I need it to also work with a straight Java array.  The reason for
> > > > doing this is simply because I want to use the same POJO with some web
> > > > services.  I am using Axis2 for web services which does not support
> > > > (yet) using a List, you must use an old-school array of objects.
> > > >
> > > > So the question is simple.  Can iBatis support an array of POJO objects
> > > > instead of using a List of POJO objects?  I know using a List would be
> > > > better, and I wish I could, but I cannot at the moment.
> > > >
> > > > Sample POJOs:
> > > >
> > > > public class ProgramVO {
> > > >
> > > >         private Integer programID;
> > > >         private ProgramActivityVO[] activities;  //Using an array
> > > > instead of a List
> > > >
> > > >         public Integer getProgramID() {
> > > >                 return programID;
> > > >         }
> > > >         public void setProgramID(Integer programID) {
> > > >                 this.programID = programID;
> > > >         }
> > > >
> > > >         public ProgramActivityVO[] getActivities() {
> > > >                 return activities;
> > > >         }
> > > >         public void setActivities(ProgramActivityVO[] activities) {
> > > >                 this.activities = activities;
> > > >         }
> > > >
> > > > }
> > > >
> > > >
> > > > public class ProgramActivityVO {
> > > >
> > > >         private Integer programActivityID;
> > > >         private Integer activityID;
> > > >
> > > >         public Integer getProgramActivityID() {
> > > >                 return programActivityID;
> > > >         }
> > > >         public void setProgramActivityID(Integer programActivityID) {
> > > >                 this.programActivityID = programActivityID;
> > > >         }
> > > >
> > > >         public Integer getActivityID() {
> > > >                 return activityID;
> > > >         }
> > > >         public void setActivityID(Integer activityID) {
> > > >                 this.activityID = activityID;
> > > >         }
> > > > }
> > > >
> > > > SQL Map:
> > > >
> > > >         <resultMap id="programResult" class="Program"
> > > > groupBy="programID">
> > > >                 <result property="programID"/>
> > > >                 <result property="activities"
> > > > resultMap="program.programActivityResult"/>
> > > >         </resultMap>
> > > >
> > > >         <resultMap id="programActivityResult" class="ProgramActivity">
> > > >                 <result property="programActivityID"
> > > > column="programActivityID" />
> > > >                 <result property="activityID" column="activityID" />
> > > >         </resultMap>
> > > >
> > > >
> > > > Regards,
> > > > Collin Peters
> > > >
> > >
> >
>

Re: Using an array instead of a List

Posted by Collin Peters <ca...@gmail.com>.
Hi Ted,

I believe since the List still exists in the VO, Axis will complain.
You would essentially end up having two versions of the VO which would
wreck havoc on having proper code using the services (i.e. you
essentially would have to have two versions of everything)

So does this mean that iBatis truly doesn't support Java arrays?  I
would find this to be pretty interesting if that is the case.  I
realize that Lists and Collections are better but an array is still a
common data structure.

Collin

On 3/2/07, Ted Schrader <te...@gmail.com> wrote:
> Hi Collin,
>
> How about a derived getter for use by the web services?  Here's what I mean:
>
> public class ProgramVO {
>
>        // programID is in here somewhere.
>
>        // List of ProgramActivityVOs
>        private List activities;
>
>        public List getActivities() {
>                return activities;
>        }
>        public void setActivities(List activities) {
>                this.activities = activities;
>        }
>
>       /**
>        * Derived getter. See getActivites().
>        *  Note: I did not compile this; it's just for illustration.
>        *  Probably could be more efficient, too.
>        */
>       public ProgramActivityVO[] getActivitiesAsArray() {
>            final ProgramActivityVO[] result =
>                   new ProgramActivityVO[getActivites().size()];
>
>            for(int i = 0; i < getActivites().size(); ++i) {
>                 result[i] = (ProgramActivityVO)getActivites().get(i);
>            }
>
>            return result;
>       }
> }
>
>
> This way, iBATIS can give you the List, and you can give Axis the
> array it needs by calling getActivitesAsArray() instead of
> getActivities().
>
> Ted
>
>
>
> On 02/03/07, Collin Peters <ca...@gmail.com> wrote:
> > I'm not sure if I follow.  After looking over the <iterate> tag, it
> > looks like it is only for adding dynamic SQL *into* a select
> > statement.  I am looking for a way to get data *out of* a select
> > statement and into a Java array.
> >
> > Do you have any examples or links of what you mean?
> >
> > On 3/2/07, Poitras Christian <Ch...@ircm.qc.ca> wrote:
> > > From what I know, arrays can be used with getters, by not setters.
> > > For instance, the <iterate> tag should work with arrays and Lists, but
> > > not resultMap.
> > >
> > > Christian
> > >
> > > -----Original Message-----
> > > From: Collin Peters [mailto:cadiolis@gmail.com]
> > > Sent: Friday, 02 March 2007 15:07
> > > To: user-java@ibatis.apache.org
> > > Subject: Using an array instead of a List
> > >
> > > This is the same question as this post, which never got fully
> > > answered:
> > > http://www.mail-archive.com/user-java@ibatis.apache.org/msg06454.html
> > >
> > > I have a POJO which has an array of another POJO in it (N+1 style from
> > > the iBatis guide).  I have gotten this to work with a java.util.List,
> > > but I need it to also work with a straight Java array.  The reason for
> > > doing this is simply because I want to use the same POJO with some web
> > > services.  I am using Axis2 for web services which does not support
> > > (yet) using a List, you must use an old-school array of objects.
> > >
> > > So the question is simple.  Can iBatis support an array of POJO objects
> > > instead of using a List of POJO objects?  I know using a List would be
> > > better, and I wish I could, but I cannot at the moment.
> > >
> > > Sample POJOs:
> > >
> > > public class ProgramVO {
> > >
> > >         private Integer programID;
> > >         private ProgramActivityVO[] activities;  //Using an array
> > > instead of a List
> > >
> > >         public Integer getProgramID() {
> > >                 return programID;
> > >         }
> > >         public void setProgramID(Integer programID) {
> > >                 this.programID = programID;
> > >         }
> > >
> > >         public ProgramActivityVO[] getActivities() {
> > >                 return activities;
> > >         }
> > >         public void setActivities(ProgramActivityVO[] activities) {
> > >                 this.activities = activities;
> > >         }
> > >
> > > }
> > >
> > >
> > > public class ProgramActivityVO {
> > >
> > >         private Integer programActivityID;
> > >         private Integer activityID;
> > >
> > >         public Integer getProgramActivityID() {
> > >                 return programActivityID;
> > >         }
> > >         public void setProgramActivityID(Integer programActivityID) {
> > >                 this.programActivityID = programActivityID;
> > >         }
> > >
> > >         public Integer getActivityID() {
> > >                 return activityID;
> > >         }
> > >         public void setActivityID(Integer activityID) {
> > >                 this.activityID = activityID;
> > >         }
> > > }
> > >
> > > SQL Map:
> > >
> > >         <resultMap id="programResult" class="Program"
> > > groupBy="programID">
> > >                 <result property="programID"/>
> > >                 <result property="activities"
> > > resultMap="program.programActivityResult"/>
> > >         </resultMap>
> > >
> > >         <resultMap id="programActivityResult" class="ProgramActivity">
> > >                 <result property="programActivityID"
> > > column="programActivityID" />
> > >                 <result property="activityID" column="activityID" />
> > >         </resultMap>
> > >
> > >
> > > Regards,
> > > Collin Peters
> > >
> >
>

Re: Using an array instead of a List

Posted by Ted Schrader <te...@gmail.com>.
Hi Collin,

How about a derived getter for use by the web services?  Here's what I mean:

public class ProgramVO {

       // programID is in here somewhere.

       // List of ProgramActivityVOs
       private List activities;

       public List getActivities() {
               return activities;
       }
       public void setActivities(List activities) {
               this.activities = activities;
       }

      /**
       * Derived getter. See getActivites().
       *  Note: I did not compile this; it's just for illustration.
       *  Probably could be more efficient, too.
       */
      public ProgramActivityVO[] getActivitiesAsArray() {
           final ProgramActivityVO[] result =
                  new ProgramActivityVO[getActivites().size()];

           for(int i = 0; i < getActivites().size(); ++i) {
                result[i] = (ProgramActivityVO)getActivites().get(i);
           }

           return result;
      }
}


This way, iBATIS can give you the List, and you can give Axis the
array it needs by calling getActivitesAsArray() instead of
getActivities().

Ted



On 02/03/07, Collin Peters <ca...@gmail.com> wrote:
> I'm not sure if I follow.  After looking over the <iterate> tag, it
> looks like it is only for adding dynamic SQL *into* a select
> statement.  I am looking for a way to get data *out of* a select
> statement and into a Java array.
>
> Do you have any examples or links of what you mean?
>
> On 3/2/07, Poitras Christian <Ch...@ircm.qc.ca> wrote:
> > From what I know, arrays can be used with getters, by not setters.
> > For instance, the <iterate> tag should work with arrays and Lists, but
> > not resultMap.
> >
> > Christian
> >
> > -----Original Message-----
> > From: Collin Peters [mailto:cadiolis@gmail.com]
> > Sent: Friday, 02 March 2007 15:07
> > To: user-java@ibatis.apache.org
> > Subject: Using an array instead of a List
> >
> > This is the same question as this post, which never got fully
> > answered:
> > http://www.mail-archive.com/user-java@ibatis.apache.org/msg06454.html
> >
> > I have a POJO which has an array of another POJO in it (N+1 style from
> > the iBatis guide).  I have gotten this to work with a java.util.List,
> > but I need it to also work with a straight Java array.  The reason for
> > doing this is simply because I want to use the same POJO with some web
> > services.  I am using Axis2 for web services which does not support
> > (yet) using a List, you must use an old-school array of objects.
> >
> > So the question is simple.  Can iBatis support an array of POJO objects
> > instead of using a List of POJO objects?  I know using a List would be
> > better, and I wish I could, but I cannot at the moment.
> >
> > Sample POJOs:
> >
> > public class ProgramVO {
> >
> >         private Integer programID;
> >         private ProgramActivityVO[] activities;  //Using an array
> > instead of a List
> >
> >         public Integer getProgramID() {
> >                 return programID;
> >         }
> >         public void setProgramID(Integer programID) {
> >                 this.programID = programID;
> >         }
> >
> >         public ProgramActivityVO[] getActivities() {
> >                 return activities;
> >         }
> >         public void setActivities(ProgramActivityVO[] activities) {
> >                 this.activities = activities;
> >         }
> >
> > }
> >
> >
> > public class ProgramActivityVO {
> >
> >         private Integer programActivityID;
> >         private Integer activityID;
> >
> >         public Integer getProgramActivityID() {
> >                 return programActivityID;
> >         }
> >         public void setProgramActivityID(Integer programActivityID) {
> >                 this.programActivityID = programActivityID;
> >         }
> >
> >         public Integer getActivityID() {
> >                 return activityID;
> >         }
> >         public void setActivityID(Integer activityID) {
> >                 this.activityID = activityID;
> >         }
> > }
> >
> > SQL Map:
> >
> >         <resultMap id="programResult" class="Program"
> > groupBy="programID">
> >                 <result property="programID"/>
> >                 <result property="activities"
> > resultMap="program.programActivityResult"/>
> >         </resultMap>
> >
> >         <resultMap id="programActivityResult" class="ProgramActivity">
> >                 <result property="programActivityID"
> > column="programActivityID" />
> >                 <result property="activityID" column="activityID" />
> >         </resultMap>
> >
> >
> > Regards,
> > Collin Peters
> >
>

Re: Using an array instead of a List

Posted by Collin Peters <ca...@gmail.com>.
I'm not sure if I follow.  After looking over the <iterate> tag, it
looks like it is only for adding dynamic SQL *into* a select
statement.  I am looking for a way to get data *out of* a select
statement and into a Java array.

Do you have any examples or links of what you mean?

On 3/2/07, Poitras Christian <Ch...@ircm.qc.ca> wrote:
> From what I know, arrays can be used with getters, by not setters.
> For instance, the <iterate> tag should work with arrays and Lists, but
> not resultMap.
>
> Christian
>
> -----Original Message-----
> From: Collin Peters [mailto:cadiolis@gmail.com]
> Sent: Friday, 02 March 2007 15:07
> To: user-java@ibatis.apache.org
> Subject: Using an array instead of a List
>
> This is the same question as this post, which never got fully
> answered:
> http://www.mail-archive.com/user-java@ibatis.apache.org/msg06454.html
>
> I have a POJO which has an array of another POJO in it (N+1 style from
> the iBatis guide).  I have gotten this to work with a java.util.List,
> but I need it to also work with a straight Java array.  The reason for
> doing this is simply because I want to use the same POJO with some web
> services.  I am using Axis2 for web services which does not support
> (yet) using a List, you must use an old-school array of objects.
>
> So the question is simple.  Can iBatis support an array of POJO objects
> instead of using a List of POJO objects?  I know using a List would be
> better, and I wish I could, but I cannot at the moment.
>
> Sample POJOs:
>
> public class ProgramVO {
>
>         private Integer programID;
>         private ProgramActivityVO[] activities;  //Using an array
> instead of a List
>
>         public Integer getProgramID() {
>                 return programID;
>         }
>         public void setProgramID(Integer programID) {
>                 this.programID = programID;
>         }
>
>         public ProgramActivityVO[] getActivities() {
>                 return activities;
>         }
>         public void setActivities(ProgramActivityVO[] activities) {
>                 this.activities = activities;
>         }
>
> }
>
>
> public class ProgramActivityVO {
>
>         private Integer programActivityID;
>         private Integer activityID;
>
>         public Integer getProgramActivityID() {
>                 return programActivityID;
>         }
>         public void setProgramActivityID(Integer programActivityID) {
>                 this.programActivityID = programActivityID;
>         }
>
>         public Integer getActivityID() {
>                 return activityID;
>         }
>         public void setActivityID(Integer activityID) {
>                 this.activityID = activityID;
>         }
> }
>
> SQL Map:
>
>         <resultMap id="programResult" class="Program"
> groupBy="programID">
>                 <result property="programID"/>
>                 <result property="activities"
> resultMap="program.programActivityResult"/>
>         </resultMap>
>
>         <resultMap id="programActivityResult" class="ProgramActivity">
>                 <result property="programActivityID"
> column="programActivityID" />
>                 <result property="activityID" column="activityID" />
>         </resultMap>
>
>
> Regards,
> Collin Peters
>

RE: Using an array instead of a List

Posted by Poitras Christian <Ch...@ircm.qc.ca>.
>From what I know, arrays can be used with getters, by not setters.
For instance, the <iterate> tag should work with arrays and Lists, but
not resultMap.

Christian

-----Original Message-----
From: Collin Peters [mailto:cadiolis@gmail.com] 
Sent: Friday, 02 March 2007 15:07
To: user-java@ibatis.apache.org
Subject: Using an array instead of a List

This is the same question as this post, which never got fully
answered:
http://www.mail-archive.com/user-java@ibatis.apache.org/msg06454.html

I have a POJO which has an array of another POJO in it (N+1 style from
the iBatis guide).  I have gotten this to work with a java.util.List,
but I need it to also work with a straight Java array.  The reason for
doing this is simply because I want to use the same POJO with some web
services.  I am using Axis2 for web services which does not support
(yet) using a List, you must use an old-school array of objects.

So the question is simple.  Can iBatis support an array of POJO objects
instead of using a List of POJO objects?  I know using a List would be
better, and I wish I could, but I cannot at the moment.

Sample POJOs:

public class ProgramVO {

	private Integer programID;
	private ProgramActivityVO[] activities;  //Using an array
instead of a List

	public Integer getProgramID() {
		return programID;
	}
	public void setProgramID(Integer programID) {
		this.programID = programID;
	}

	public ProgramActivityVO[] getActivities() {
		return activities;
	}
	public void setActivities(ProgramActivityVO[] activities) {
		this.activities = activities;
	}

}


public class ProgramActivityVO {

	private Integer programActivityID;
	private Integer activityID;

	public Integer getProgramActivityID() {
		return programActivityID;
	}
	public void setProgramActivityID(Integer programActivityID) {
		this.programActivityID = programActivityID;
	}
	
	public Integer getActivityID() {
		return activityID;
	}
	public void setActivityID(Integer activityID) {
		this.activityID = activityID;
	}
}

SQL Map:

	<resultMap id="programResult" class="Program"
groupBy="programID">
		<result property="programID"/>
		<result property="activities"
resultMap="program.programActivityResult"/>
	</resultMap>

	<resultMap id="programActivityResult" class="ProgramActivity">
		<result property="programActivityID"
column="programActivityID" />
		<result property="activityID" column="activityID" />
	</resultMap>


Regards,
Collin Peters