Using a Map instead of List

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Using a Map instead of List

Nicolai Willems
Hello MyBatis

After reading the user manual once more, I couldn't find a way of
making a Map instead of a List.
Is it possible to have a statement map to a Map<K,V> instead of a
List<T>?

To explain my case:
I have an object with 3 types of statistics bound to it, the three
statistic's objects is not join-able so I need to have three "lists".
All statistics objects have a timestamp and I would like to use that
as key.

Thanks in advance
Nicolai Willems
Reply | Threaded
Open this post in threaded view
|

RE: Using a Map instead of List

Poitras Christian
Hi,

Your best bet is to use a ResultHandler. ResultHandlers allow you to transform a database query into any java objects.
I've created an example here that returns a Map.
http://code.google.com/p/mybatis/wiki/ResultHandlerExample

Christian

-----Message d'origine-----
De : [hidden email] [mailto:[hidden email]] De la part de Nicolai Willems
Envoyé : October-15-10 4:06 AM
À : mybatis-user
Objet : Using a Map instead of List

Hello MyBatis

After reading the user manual once more, I couldn't find a way of
making a Map instead of a List.
Is it possible to have a statement map to a Map<K,V> instead of a
List<T>?

To explain my case:
I have an object with 3 types of statistics bound to it, the three
statistic's objects is not join-able so I need to have three "lists".
All statistics objects have a timestamp and I would like to use that
as key.

Thanks in advance
Nicolai Willems
Reply | Threaded
Open this post in threaded view
|

Re: Using a Map instead of List

Nicolai Willems
Hi again.

Thanks for the nice reply, this really is the bomb.
Can this be achieved when using Mapper interfaces? I feel very comfortable with the type safety of my interfaces.

I guess it would be cool if you could tell a ResultMap to be handled by some resultHandler, but I don't know how practical it would be.

/Nicolai

On Fri, Oct 15, 2010 at 1:44 PM, Poitras Christian <[hidden email]> wrote:
Hi,

Your best bet is to use a ResultHandler. ResultHandlers allow you to transform a database query into any java objects.
I've created an example here that returns a Map.
http://code.google.com/p/mybatis/wiki/ResultHandlerExample

Christian

-----Message d'origine-----
De : [hidden email] [mailto:[hidden email]] De la part de Nicolai Willems
Envoyé : October-15-10 4:06 AM
À : mybatis-user
Objet : Using a Map instead of List

Hello MyBatis

After reading the user manual once more, I couldn't find a way of
making a Map instead of a List.
Is it possible to have a statement map to a Map<K,V> instead of a
List<T>?

To explain my case:
I have an object with 3 types of statistics bound to it, the three
statistic's objects is not join-able so I need to have three "lists".
All statistics objects have a timestamp and I would like to use that
as key.

Thanks in advance
Nicolai Willems

Reply | Threaded
Open this post in threaded view
|

Re: Using a Map instead of List

Jackie Li
In reply to this post by Nicolai Willems
you can use collection property. create a map class, timestamp as the key, and all your other needed statistics into the collection property object.

Cheers,
Jackie


On Fri, Oct 15, 2010 at 16:06, Nicolai Willems <[hidden email]> wrote:
Hello MyBatis

After reading the user manual once more, I couldn't find a way of
making a Map instead of a List.
Is it possible to have a statement map to a Map<K,V> instead of a
List<T>?

To explain my case:
I have an object with 3 types of statistics bound to it, the three
statistic's objects is not join-able so I need to have three "lists".
All statistics objects have a timestamp and I would like to use that
as key.

Thanks in advance
Nicolai Willems

Reply | Threaded
Open this post in threaded view
|

Re: Using a Map instead of List

Nicolai Willems
I'm not sure I follow you on this.

"Create a map class" - like implement the Map interface in my own class or make a wrapper class?

I think I found a good solution to my problem. After a long hard think I found out that I needed two columns for the key, but one of these columns is a property of another object - yes this is a little confusing.
But I'll try:
I have an object A with a name property, this object holds a Map<Date, Statistics> - where precision on the date is bound by a granularity(so it needs to be stripped).
So the name and date property is the Key for the statistics object. And my final result should be a List<A>.

With the solution provided by Poitras I solved it by having two associations and a single result(the time property), I will report back when it works :)

/Nicolai


On Mon, Oct 18, 2010 at 10:49 AM, lwpro <[hidden email]> wrote:
you can use collection property. create a map class, timestamp as the key, and all your other needed statistics into the collection property object.
 
Cheers,
Jackie



On Fri, Oct 15, 2010 at 16:06, Nicolai Willems <[hidden email]> wrote:
Hello MyBatis

After reading the user manual once more, I couldn't find a way of
making a Map instead of a List.
Is it possible to have a statement map to a Map<K,V> instead of a
List<T>?

To explain my case:
I have an object with 3 types of statistics bound to it, the three
statistic's objects is not join-able so I need to have three "lists".
All statistics objects have a timestamp and I would like to use that
as key.

Thanks in advance
Nicolai Willems