Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8260821
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T03:11:13+00:00 2026-06-08T03:11:13+00:00

I would like to map data to java.util.Map attribute with MyBatis. I have simple

  • 0

I would like to map data to java.util.Map attribute with MyBatis. I have simple POJOs like this:

public class Bar {
     ...fields
}

public class Foo {
    private Map<String, Bar> bars;

   public Foo() {
        bars = new HashMap<String, Bar>();
   }

   ...

}

How can I map data to bars with MyBatis? Example below doesn’t work because it sets always new map to field.

<resultMap id="fooResultMap" type="Foo">
   ...attributes
    <association property="bars" resultMap="barResultMap" />
</resultMap>

<resultMap id="barResultMap" type="map">
        <result property="key" column="bar_key" />
  <association property="value" javaType="Bar">
    ...attributes
  </association>
</resultMap>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T03:11:15+00:00Added an answer on June 8, 2026 at 3:11 am

    I’ve done some research on this and asked on the MyBatis Google group, as I’d be interested in how to do this myself.

    At the current time, this appears to not be possible. You can easily make it work if you want a List<Bar> in the Foo object using the <collection> mapping.

    I tried using <collection> to return a Map, but it doesn’t understand what I’m asking it to do.

    The only way I know to do this right now is to manage it yourself with two queries/mappings – one to populate Foo with all its fields other than its collection of Bars. Then a query mapping like the one listed below to pull back all Bars in a Map and plug that into your Foo object yourself:

    @Select("SELECT bar_id, bar_name FROM bar WHERE foo_id = #{id}")
    @MapKey("bar_name")
    Map<String, Bar> getBarsById(int id);
    

    This will return a Map with one entry for each bar, where the key to the entry is the value of the “bar_name” column.

    The MyBatis team recommended putting in an issue request for this feature in a future release.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this class which i would like to map: public class Contract {
I have a terrain map which i would like to represent some data in.
I have this table structure and would like to map it using Fluent Hibernate
I have an XML file that I would like to map some attributes of
I would like to implement a map simmilar to this: http://gmaps-utility-library.googlecode.com/svn/trunk/markerclusterer/1.0/examples/advanced_example.html One of the
I would like to embed Clojure code into Java. This site was helpful in
import java.io.*; import java.util.*; public class Sort { public static void main(String[] args) throws
I would like to represent matrix-like data in a suitable data-structure in Java. The
I'm looking for a data structure like com.google.common.collect.SortedSetMultimap which additionally implements the java.util.NavigableMap interface.
I have a list of name/value pairs that I would like to map into

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.