MyBatis MapKey for nested collection

2,317 views
Skip to first unread message

Александр Свиридов

unread,
Feb 3, 2015, 12:41:00 PM2/3/15
to mybati...@googlegroups.com

I have the following java code:

class Man{
int id;
HashMap<String,Car> cars;
//getters+setters
}

class Car{
int id;
String model;
//getters+setters
}

I have the following xml mapper

<resultMap id="readItemsRM" type="Man">
<id property="id" column="manId"/>
<collection property="cars" ofType="Car">
<id property="id" column="carId"/>
<result property="model" column="carModel"/>
</collection>
</resultMap>

In interface mapper I have:

@MapKey("id")
publicHashMap<Integer,Man> select();

So when I get Map of class Man the key for map is man id. That's ok. AND I NEED THIS MAP (id-man). At the same time I need one more map (car model-model).

For example I want:

HashMap<Integer,Man> men=mapper.select();
//to get BMW car of man with id 100
Car car=men.get(100).getCars().get("BMW");

So my question is how to set the second @MapKey("model") for Map using at the same time with @MapKey("id")?
By other words how to set @MapKey for nested collection?

Condor

unread,
Feb 3, 2015, 4:15:38 PM2/3/15
to mybati...@googlegroups.com
Unfortunately MyBatis doesn't support this.

You could construct a list of AbstractMap.SimpleEntry instances using result map collection and then use new HashMap().entrySet().addAll(list) to construct a map.

Александр Свиридов

unread,
Feb 4, 2015, 12:54:58 AM2/4/15
to mybati...@googlegroups.com
It's very sad and not understandable for me. The problem is that developers put @MapKey() in interface. I dont' know why they did this but it was wrong solution as it's clear that there can be more that one Map. The solution pf this problem is very simple - move MapKey from interface to resultMap so it could be used in association,collections etc.

Can I hope that developers fix it?


Вторник, 03 февраля 2015, 13:15 -08:00 от Condor <aveg...@gmail.com>:
Unfortunately MyBatis doesn't support this.

You could construct a list of AbstractMap.SimpleEntry instances using result map collection and then use new HashMap().entrySet().addAll(list) to construct a map.

--
You received this message because you are subscribed to the Google Groups "mybatis-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mybatis-user...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Condor

unread,
Feb 4, 2015, 2:12:31 AM2/4/15
to mybati...@googlegroups.com
I agree. It would be nice if the collection tag supported a mapKey attribute to fill a map instead of a list.

I suggest you post a feature request at:
https://github.com/mybatis/mybatis-3/issues

Александр Свиридов

unread,
Feb 4, 2015, 3:28:12 AM2/4/15
to mybati...@googlegroups.com
Thank you. I've opened issue: https://github.com/mybatis/mybatis-3/issues/345


Вторник, 03 февраля 2015, 23:12 -08:00 от Condor <aveg...@gmail.com>:
I agree. It would be nice if the collection tag supported a mapKey attribute to fill a map instead of a list.

I suggest you post a feature request at:
https://github.com/mybatis/mybatis-3/issues

Александр Свиридов

unread,
Feb 10, 2015, 11:45:17 AM2/10/15
to mybati...@googlegroups.com
Ok. I posted there. What next? Will you implement it? I don't hurry you as I don't have any rights to do it. But I ask to answer if you are going to implement and if yes then when? I need to know for my plans.
Reply all
Reply to author
Forward
0 new messages