title: Hibernate collection mapping: map<String, Entity2> property in Entity1
body: <p>I am trying to map the following entities with (legacy) DB tables and have trouble with mapping a map:</p>
  
  <pre><code>public class Configuration {
  
      private Long configurationId;
      private String code;
      private Long index;
  
      private Map&lt;String, ConfigurationParam&gt; configurationParams;
      ...
  }
  </code></pre>
  
  <p>and </p>
  
  <pre><code>public class ConfigurationParam {
  
      private Long configurationId; // foreign key
      private String code;
      private String value;
      ...
  }
  </code></pre>
  
  <p>As you can see, a <em>configuration</em> object holds a map of <em>configurationParam</em> objects. The key of the map is the code property of <em>ConfigurationParam</em> . Please note that the properties <em>code</em> of each entities are not related to each other (legacy schema :( )</p>
  
  <p>The hibernate mapping looks like this:</p>
  
  <pre><code>&lt;class name="Configuration" table="CONFIGURATION" dynamic-update="true"&gt;
      &lt;id name="configurationId" column="CONFIGURATIONID"&gt;
        &lt;generator class="assigned" /&gt;
      &lt;/id&gt;
      &lt;property name="configurationSetId" column="CONFIGURATIONSETID" /&gt; 
      &lt;property name="code" column="CODE" /&gt; 
      &lt;property name="index" column="INDX" /&gt; 
  
      &lt;map name="configurationParams" lazy="true" table="CONFIGURATIONPARAM"  fetch="select"  
          batch-sie="10"&gt;
          &lt;key column="CONFIGURATIONID" /&gt;
          &lt;map-key type="string" column="CODE"/&gt;
          &lt;element type="ConfigurationParam"&gt;&lt;/element&gt;
      &lt;/map&gt;
    &lt;/class&gt;
  </code></pre>
  
  <p>and</p>
  
  <pre><code>&lt;class name="ConfigurationParam" table="CONFIGURATIONPARAM" dynamic-update="true"&gt;
      &lt;composite-id class="ConfigurationParamId" mapped="true"&gt;
          &lt;key-property name="configurationId"/&gt;
          &lt;key-property name="code"/&gt;
      &lt;/composite-id&gt;
      &lt;property name="configurationId" column="CONFIGURATIONID" /&gt; 
      &lt;property name="code" column="CODE" /&gt; 
      &lt;property name="value" column="VALUE" /&gt;
  &lt;/class&gt;
  </code></pre>
  
  <p>However, the params are not loaded in the desired way, also not if I turn on eager fetching and also not if I try to fetch Configurations with a named query and <em>join fetch configurationParams</em>.</p>
  
  <p>Why is this the case? And how to map the association correctly?</p>
  
  <p>I already had some ideas what the problem could be this seems not to be the problem (at least alone):</p>
  
  <ol>
  <li><p>both entities have a property named <em>code</em> , probably the wrong one is used as <em>map-key</em> by Hibernate?
  (It should of course use the <em>code</em> property of the ConfigurationParam entity.)</p></li>
  <li><p>ConfigurationParam.code is not unique. As you see, the primary key of ConfigurationParam is a composite.
  However, since it is a map maintained by Configuration whose primary key is the second part of this composite, I think this should be OK. Hibernate did not complain anyway.</p></li>
  <li><p>ConfigurationParam.code is already mapped as property in the entity itself. Probably, then you cannot use it as map key anymore? However, also this Hibernate would tell me, I guess?</p></li>
  </ol>
  
  <p>I tried to find answers in the Hibernate documentation (you know cats and such things) and read a lot of other questions here but did not find an answer to this particular issue</p>