Collection and List of Strings

6,970 views
Skip to first unread message

q111111

unread,
Aug 13, 2010, 9:37:36 AM8/13/10
to mybatis-user
I am missing something but I am not sure what.
I want to fill a list of strings.
Currently, the SimpleDomain does return a name with a null list.

Java:
public class SimpleDomain
{
String name;
List<String> someStrings;
//getters and setters here
}

Mapper:
<select id="MySuperQuery" resultMap="MyMap">
select tbl1.lname as NAME, tlb2.lstring as MYSTRING
from tbl1
left outer join tbl2 on tbl1.id = tbl2.id
</select>

<resultMap id="MyMap" type="SimpleDomain">
<result property="name" column="NAME"/>
<collection property="someStrings" javaType="list" column="MYSTRING"
ofType="string"/>
</resultMap>

Thank you.

Simone Tripodi

unread,
Aug 13, 2010, 10:29:34 AM8/13/10
to mybati...@googlegroups.com
Hi!
did you try with javaType="java.util.ArrayList" instead of javaType="list" ?
let us know!
Simo

http://people.apache.org/~simonetripodi/
http://www.99soft.org/

q111111

unread,
Aug 13, 2010, 10:54:21 AM8/13/10
to mybatis-user
Thank you for the suggestion.

Still returning a null list.

Before the question is asked by someone else ...
Running the query straight in database returns 6 rows (Tbl1 has 1 row
and Tbl2 has 6).

On Aug 13, 10:29 am, Simone Tripodi <simone.trip...@gmail.com> wrote:
> Hi!
> did you try with javaType="java.util.ArrayList" instead of javaType="list" ?
> let us know!
> Simo
>
> http://people.apache.org/~simonetripodi/http://www.99soft.org/
>
>
>
> On Fri, Aug 13, 2010 at 3:37 PM, q111111 <q211222...@yahoo.com> wrote:
> > I am missing something but I am not sure what.
> > I want to fill a list of strings.
> > Currently, the SimpleDomain does return a name with a null list.
>
> > Java:
> > public class SimpleDomain
> > {
> >    String name;
> >    List<String> someStrings;
> >    //getters and setters here
> > }
>
> > Mapper:
> > <select id="MySuperQuery" resultMap="MyMap">
> >     select tbl1.lname as NAME, tlb2.lstring as MYSTRING
> >        from tbl1
> >        left outer join tbl2 on tbl1.id = tbl2.id
> > </select>
>
> > <resultMap id="MyMap" type="SimpleDomain">
> >  <result property="name" column="NAME"/>
> >  <collection property="someStrings" javaType="list" column="MYSTRING"
> > ofType="string"/>
> > </resultMap>
>
> > Thank you.- Hide quoted text -
>
> - Show quoted text -

Poitras Christian

unread,
Aug 13, 2010, 11:05:53 AM8/13/10
to mybati...@googlegroups.com
Try removing javaType from the <collection> section in your resultMap.

Personnaly, I don't use javaType with collection since javaType is usually guessed correctly by reflection.
Since your property is defined to be List, MyBatis will supply a valid implementation for you.

q111111

unread,
Aug 13, 2010, 11:16:57 AM8/13/10
to mybatis-user
Thank you for the suggestion.

Still returning a null list.

In case it matters, I am using myBatis 3.0.1 and Oracle 11g.

Previously, I did successfully implement my mapper with two seperate
select statements ... one to fill name and one to fill someStrings
from a second query.
ex: <collection property="someStrings" javaType="list"
column="MYSTRING" ofType="string" select="MySuperQuery2"/>

Now I am trying to get it down to a single SQL statement
implementation.
I'm keeping my fingers crossed.

On Aug 13, 11:05 am, Poitras Christian <Christian.Poit...@ircm.qc.ca>
wrote:
> > - Show quoted text -- Hide quoted text -

Poitras Christian

unread,
Aug 13, 2010, 11:22:17 AM8/13/10
to mybati...@googlegroups.com
You can try to replace <result property="name" column="NAME"/> by <id property="name" column="NAME"/>.

I find that bizarre that you are getting null. It is as if MyBatis does not try to set the property at all...

Christian

q111111

unread,
Aug 13, 2010, 11:43:56 AM8/13/10
to mybatis-user
I changed to use <id property="name" column="NAME"/>.

I would like to say we made some progress ...
When I do this, the List contains one String and it is the same value
as the name it is pulling from the database.
Ex: name="Bob", someStrings={"Bob"}
I'm expecting to see someStrings={"U","V","W","X","Y","Z"}

Just a thought ...
Do I need to add "id" (since part of Where clause) to my SimpleDomain
and then use the <id> tag on it?
I didn't think so but I thought I would ask.

On Aug 13, 11:22 am, Poitras Christian <Christian.Poit...@ircm.qc.ca>

Poitras Christian

unread,
Aug 13, 2010, 12:06:51 PM8/13/10
to mybati...@googlegroups.com
For sure, if your SimpleDomain has some id field, you should use that field for the <id> in your resultMap - not required but it is more logical.

Still, I am unsure about what MyBatis does in your case...

q111111

unread,
Aug 13, 2010, 2:17:35 PM8/13/10
to mybatis-user
I added the id field to SimpleDomain and added <id property="id"
column="id"/> to mapper.
The list is still filled with the name (the first thing I selected).
Ex: name="Bob", someStrings={"Bob"}

I added the id field to the what I am selecting from the database and
then noticed that the list contained that id.
So it seems whatever is first selected, goes into my list.

On a whim, I changed the order of the things selected so the items
for the list went in first.
Ex: <select id="MySuperQuery" resultMap="MyMap">
select tlb2.lstring as MYSTRING, tbl1.lname as NAME from tbl1
left outer join tbl2 on tbl1.id = tbl2.id
</select>

<resultMap id="MySuperQueryMap"
type="gov.ssa.epecs.domain.SimpleDomain">
<id property="id" column="id" />
<result property="name" column="NAME"/>
<collection property="someStrings" column="MYSTRING" ofType="string"/
>
</resultMap>

Now my list contains only the first correct string from the database.
So I am seeing name="Bob", someStrings={"U"} but I am expecting
={"U","V","W","X","Y","Z"}
So I am closer. I don't know why the order of the select matters but
it seems to (if that gives a clue).

On Aug 13, 12:06 pm, Poitras Christian <Christian.Poit...@ircm.qc.ca>
wrote:

Poitras Christian

unread,
Aug 16, 2010, 9:22:17 AM8/16/10
to mybati...@googlegroups.com
I've made a quick test to see what is the appropriate way to go.
Simply put a <result> element inside the <collection> element.

<resultMap id="MySuperQueryMap"
type="gov.ssa.epecs.domain.SimpleDomain">
<id property="id" column="id" />
<result property="name" column="NAME"/>

<collection property="someStrings" ofType="string">
<result column="MYSTRING"/>
</collection>
</resultMap>

I guess it would be better and more comprehensible if you could use <collection property="someStrings" column="MYSTRING" ofType="string"/>.
So you can add a bug report for this at http://code.google.com/p/mybatis/wiki/Issues.

q111111

unread,
Aug 16, 2010, 12:24:41 PM8/16/10
to mybatis-user
That worked!!!
Seeing the solution now makes sense.

Thank you for solution.
Thank you to all for helping out.


On Aug 16, 9:22 am, Poitras Christian <Christian.Poit...@ircm.qc.ca>
wrote:
> I've made a quick test to see what is the appropriate way to go.
> Simply put a <result> element inside the <collection> element.
>
> <resultMap id="MySuperQueryMap"
> type="gov.ssa.epecs.domain.SimpleDomain">
>   <id property="id" column="id" />
>   <result property="name" column="NAME"/>
>   <collection property="someStrings" ofType="string">
>     <result column="MYSTRING"/>
>   </collection>
> </resultMap>
>
> I guess it would be better and more comprehensible if you could use <collection property="someStrings" column="MYSTRING" ofType="string"/>.
> So you can add a bug report for this
>

bostone

unread,
Oct 22, 2012, 2:40:41 PM10/22/12
to mybati...@googlegroups.com
Just as a quick note - this is a good solution but it only worked for me after adding javaType="ArrayList" to <collection/>. Otherwise I was getting a single string instead of the array
Reply all
Reply to author
Forward
0 new messages