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?
@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.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.
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