Quantcast

What are the supported return types from Mapper interface methods?

classic Classic list List threaded Threaded
10 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

What are the supported return types from Mapper interface methods?

icfantv
I am using the @Select annotation and my SQL is using "SELECT DISTINCT ...."  My interface method defines a return type of SortedSet<String>.

Mybatis is barfing saying essentially that it is only expecting one result to be returned and not the six that I am returning.  When I change it to List<String> it's fine.

The weird thing is that I am using SortedSet<my_bean> elsewhere but that SQL is being executed in XML and it works correctly.

What are the supported return types from Mapper interface methods?  Is my problem the SortedSet or is it something else?

Thanks.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: What are the supported return types from Mapper interface methods?

icfantv
An update:

It appears that the keyword DISTINCT is the culprit here.  Where I'm using SortedSet and it works, I don't have DISTINCT in my select statement and adding it causes the error.

Why would mybatis interpret the distinct keyword in a select statement as only returning one row?  Is this a bug?
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: What are the supported return types from Mapper interface methods?

Eduardo Macarron
MyBatis supports any result type for selectOne, List for selectList
and Map for selectMap.

So:
- When a return type List is found it internally calls selectList
- When a Map is found and a MapKey annotation is present it calls
selectList
- Else it calls selectOne

On 3 feb, 03:39, icfantv <[hidden email]> wrote:

> An update:
>
> It appears that the keyword DISTINCT is the culprit here.  Where I'm using
> SortedSet and it works, I don't have DISTINCT in my select statement and
> adding it causes the error.
>
> Why would mybatis interpret the distinct keyword in a select statement as
> only returning one row?  Is this a bug?
>
> --
> View this message in context:http://mybatis-user.963551.n3.nabble.com/What-are-the-supported-retur...
> Sent from the mybatis-user mailing list archive at Nabble.com.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: What are the supported return types from Mapper interface methods?

icfantv
But "SELECT DISTINCT" doesn't mean "select one."  It means select the unique items from this query result, i.e., remove all the duplicates.

I think this is a bug if MyBatis treats "SELECT DISTINCT" as "select one" because in Java, a List implies order, but not uniqueness.  A Set implies uniqueness, but not order.  And a SortedSet implies both order and uniqueness.

What's the point of having a SortedSet that only contains one element?  You might as well just return the object.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: What are the supported return types from Mapper interface methods?

icfantv
It appears we're having this discussion in multiple locations.  We can close this thread due to issue 510 being opened:

http://code.google.com/p/mybatis/issues/detail?id=510
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: What are the supported return types from Mapper interface methods?

anirvan
that thread status actually indicates that the feature request has been accommodated in the current snapshot.

have you tried that?

On Fri, Feb 3, 2012 at 9:43 PM, icfantv <[hidden email]> wrote:
It appears we're having this discussion in multiple locations.  We can close
this thread due to issue 510 being opened:

http://code.google.com/p/mybatis/issues/detail?id=510
http://code.google.com/p/mybatis/issues/detail?id=510


--
View this message in context: http://mybatis-user.963551.n3.nabble.com/What-are-the-supported-return-types-from-Mapper-interface-methods-tp3711701p3713535.html
Sent from the mybatis-user mailing list archive at Nabble.com.

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: What are the supported return types from Mapper interface methods?

Eduardo Macarron
Hi All,

Yes. I think icfantv point is right. MyBatis can only return Lists and
allowing other objects will require an API change to add selectVector,
selectSet, selectSortedSet, selectLinkedList.... :) A bit overbloated
isn't it?

But why not support that for mappers? I cannot find any reason for
that, the benefit is obvious and the code change is minor.

It is already changed in the snapshot and available for download.

Mappers now support any return object that implements collection or an
array. Hence these all work:
List<Author> selectAuthors();
Set<Autor> selectAuthors();
HashSet<Author> selectAuthors();
LinkedList<Author> selectAuthors();
Author[] selectAuthors();
...

Thanks for the idea icfantv!. Tests and comments are appreciated.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: What are the supported return types from Mapper interface methods?

Filipe Sousa
I have a few cases where returning a set will be helpful. No longer need to convert a list into a set :)
I did a few tests and it seems to work.

Thank you.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: What are the supported return types from Mapper interface methods?

Frank Martínez
In reply to this post by Eduardo Macarron

Hola Eduardo,

I support only java.util.List in the scala API by now, but this change opens the posibility of using scala collections directly. I will do some tests, scala collections are not directly java collections, but ones added support for java collectios it will be easy to extend it to the scala ones.

Cheers,
Frank.

El 04/02/2012 03:42, "Eduardo" <[hidden email]> escribió:
Hi All,

Yes. I think icfantv point is right. MyBatis can only return Lists and
allowing other objects will require an API change to add selectVector,
selectSet, selectSortedSet, selectLinkedList.... :) A bit overbloated
isn't it?

But why not support that for mappers? I cannot find any reason for
that, the benefit is obvious and the code change is minor.

It is already changed in the snapshot and available for download.

Mappers now support any return object that implements collection or an
array. Hence these all work:
List<Author> selectAuthors();
Set<Autor> selectAuthors();
HashSet<Author> selectAuthors();
LinkedList<Author> selectAuthors();
Author[] selectAuthors();
...

Thanks for the idea icfantv!. Tests and comments are appreciated.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: What are the supported return types from Mapper interface methods?

Eduardo Macarron
Sounds good Frank!

On 5 feb, 14:18, Frank Martínez <[hidden email]> wrote:

> Hola Eduardo,
>
> I support only java.util.List in the scala API by now, but this change
> opens the posibility of using scala collections directly. I will do some
> tests, scala collections are not directly java collections, but ones added
> support for java collectios it will be easy to extend it to the scala ones.
>
> Cheers,
> Frank.
> El 04/02/2012 03:42, "Eduardo" <[hidden email]> escribió:
>
>
>
>
>
>
>
> > Hi All,
>
> > Yes. I think icfantv point is right. MyBatis can only return Lists and
> > allowing other objects will require an API change to add selectVector,
> > selectSet, selectSortedSet, selectLinkedList.... :) A bit overbloated
> > isn't it?
>
> > But why not support that for mappers? I cannot find any reason for
> > that, the benefit is obvious and the code change is minor.
>
> > It is already changed in the snapshot and available for download.
>
> > Mappers now support any return object that implements collection or an
> > array. Hence these all work:
> > List<Author> selectAuthors();
> > Set<Autor> selectAuthors();
> > HashSet<Author> selectAuthors();
> > LinkedList<Author> selectAuthors();
> > Author[] selectAuthors();
> > ...
>
> > Thanks for the idea icfantv!. Tests and comments are appreciated.
Loading...