title: How to map java.util.Map with associations in hibernate xml?
body: <p>I have entity <code>Person</code> as follows:</p>
  
  <pre><code>@Entity
  @Table(name = "Person")
  @Inheritance(strategy = InheritanceType.JOINED)
  public class Person implements Serializable {
      @Id
      @GeneratedValue(strategy = GenerationType.IDENTITY)
      private Long id;
      @Column(name = "personName", length = 16, nullable = false)
      private String name;
  .....................
  }
  </code></pre>
  
  <p>I have entity <code>Teacher</code> as follows:</p>
  
  <pre><code>@Entity
  @Table(name = "teacher")
  @PrimaryKeyJoinColumn(name = "PersonId")
  public class Teacher extends Person implements Serializable {
  
      private Map&lt;String, Child&gt; childByName = new HashMap&lt;&gt;();
          ......................................
  }
  </code></pre>
  
  <p>I have entity <code>Child</code> as follows:</p>
  
  <pre><code>@Entity
  @Table(name = "CHILD")
  @PrimaryKeyJoinColumn(name = "PersonId")
  public class Child extends Person implements Serializable {
          ..............................
  }
  </code></pre>
  
  <p>Map <code>Teacher.childByName</code> should map <code>name</code> property of <code>Child</code> entity as a key and <code>Child</code> entity as a value. The relationship between <code>Teacher</code> and <code>Child</code> is one-to-many.</p>
  
  <p>I need this mapping to be in xml format.
  For now I did this in Person.hbm.xml :</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;!DOCTYPE hibernate-mapping PUBLIC
  "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
  "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"&gt;
  
  &lt;hibernate-mapping&gt;
      &lt;class name="com.masterhibernate.SimpleHibernateDemo.Person"
          table="Person"&gt;
          &lt;cache usage="read-write" /&gt;
  
          &lt;id name="id" column="id"&gt;
              &lt;generator class="native" /&gt;
          &lt;/id&gt;
  
          &lt;property name="name"&gt;
              &lt;column name="name" length="24" not-null="true" /&gt;
          &lt;/property&gt;
          &lt;property name="surname"&gt;
              &lt;column name="surname" length="36"&gt;&lt;/column&gt;
          &lt;/property&gt;
          &lt;property name="address"&gt;
              &lt;column name="address" length="32"&gt;&lt;/column&gt;
          &lt;/property&gt;
          &lt;joined-subclass name="com.masterhibernate.SimpleHibernateDemo.Parent"
              table="Parent"&gt;
              &lt;key column="person_id" foreign-key="parent_person" /&gt;
              &lt;property name="job" column="WorkPlace" length="22" type="string" /&gt;
              &lt;set name="children" inverse="true" cascade="save-update" lazy="true"&gt;
                  &lt;cache usage="read-write" /&gt;
                  &lt;!-- specifies foreign key column of child table --&gt;
                  &lt;key column="ParentPK" /&gt;
                  &lt;one-to-many class="com.masterhibernate.SimpleHibernateDemo.Child" /&gt;
              &lt;/set&gt;
          &lt;/joined-subclass&gt;
          &lt;joined-subclass name="com.masterhibernate.SimpleHibernateDemo.Child"
              table="Child"&gt;
              &lt;key column="person_id" foreign-key="child_person" /&gt;
              &lt;property name="toy" column="toy" length="55" type="string" /&gt;
              &lt;many-to-one name="parent"
                  class="com.masterhibernate.SimpleHibernateDemo.Parent" column="ParentPK"
                  lazy="false" fetch="join" foreign-key="child_parent" /&gt;
              &lt;set name="teachers" table="TeacherPupil" inverse="true" lazy="true"&gt;
                  &lt;key column="child_id" foreign-key="teacherPupil_child" /&gt;
                  &lt;many-to-many class="com.masterhibernate.SimpleHibernateDemo.Teacher"
                      column="teacher_id" /&gt;
              &lt;/set&gt;
          &lt;/joined-subclass&gt;
          &lt;joined-subclass name="com.masterhibernate.SimpleHibernateDemo.Teacher"
              table="Teacher"&gt;
              &lt;key column="person_id" foreign-key="teacher_person" /&gt;
              &lt;property name="subject" column="subject" length="25" type="string" /&gt;
              &lt;set name="children" table="TeacherPupil" lazy="false" fetch="join"&gt;
                  &lt;cache usage="read-write" /&gt;
                  &lt;key column="teacher_id" foreign-key="teacherPupil_teacher" /&gt;
                  &lt;many-to-many class="com.masterhibernate.SimpleHibernateDemo.Child"
                      column="child_id" /&gt;
              &lt;/set&gt;
              &lt;map name="childByName" table="Child_By_Name" embed-xml="true"&gt;
                  &lt;key column="childByName_id" /&gt;
                  &lt;index column="childName" type="string" /&gt;
                  &lt;one-to-many class="com.masterhibernate.SimpleHibernateDemo.Child" /&gt;
              &lt;/map&gt;
          &lt;/joined-subclass&gt;
      &lt;/class&gt;
  &lt;/hibernate-mapping&gt;
  </code></pre>
  
  <p>Unfortunately <code>&lt;map&gt;</code> doesn't even create separate table called <code>Child_By_Name</code>. Instead it creates columns <code>childByName</code> and <code>childName</code> in table <code>Child</code>. This is very strange.</p>
  
  <p>So, how to map my <code>childByName</code> Map to be in separate table to reference <code>Child</code> as its value?</p>