title: Writing a HashMap to database using Hibernate
body: <p>I am trying to write the values of hashmap to a database using Hibernate.</p>
  
  <p>MY POJO class
  package org.rahul;</p>
  
  <pre><code>import java.util.HashMap;
  import java.util.Map;
  
  import javax.persistence.ElementCollection;
  import javax.persistence.Entity;
  import javax.persistence.GeneratedValue;
  import javax.persistence.Id;
  import javax.persistence.JoinColumn;
  import javax.persistence.JoinTable;
  import javax.persistence.MapKeyColumn;
  import javax.persistence.Table;
  
  @Entity
  @Table(name = "hash")
  public class HashMapExample {
  
    @Id
    @GeneratedValue
    private int id;
  
    @ElementCollection
    @MapKeyColumn(name = "key_key")
    @JoinTable(name = "example_attributes", joinColumns =
                                    @JoinColumn(name"example_id"))
    private Map&lt;String, String&gt; map = new HashMap&lt;String, String&gt;();
  
    public int getId() {
      return id;
    }
  
    public void setId(int id) {
      this.id = id;
    }
  
    public Map&lt;String, String&gt; getMap() {
      return map;
    }
  
    public void setMap(Map&lt;String, String&gt; map) {
      this.map = map;
    }
  
  }
  </code></pre>
  
  <p>Main Class </p>
  
  <pre><code>package org.rahul.hibernate;
  
  import java.util.HashMap;
  import java.util.Map;
  
  import org.hibernate.Session;
  import org.hibernate.SessionFactory;
  import org.hibernate.cfg.AnnotationConfiguration;
  import org.rahul.HashMapExample;
  
  public class HashMapMain {
  
      /**
       * @param args
       */
      public static void main(String[] args) {
          // TODO Auto-generated method stub
          SessionFactory sessionFactory = new AnnotationConfiguration()
                  .configure("hibernate.cfg.xml").buildSessionFactory();
          Session session = sessionFactory.openSession();
          session.beginTransaction();
  
          String name = "Rahul";
          String company = "Memoir";
  
          Map&lt;String, String&gt; map = new HashMap&lt;String, String&gt;();
          map.put(name, company);
          Map&lt;String, String&gt; map1 = new HashMap&lt;String, String&gt;();
          map1.put("XYZ", "ABC");
  
          HashMapExample hash = new HashMapExample();
          HashMapExample hash1 = new HashMapExample();
  
          // hash.setId(2);
          hash1.setMap(map);
          hash.setMap(map1);
          session.getTransaction();
          session.save(hash);
          session.save(hash1);
          session.getTransaction().commit();
  
      }
  }
  </code></pre>
  
  <p>I expect to get the table with a column containing the key value pair but , the I get the key values as different columns . </p>
  
  <pre><code>example_attribut table description : 
    CREATE TABLE `example_attributes` 
    ( 
       `example_id` INT(11) NOT NULL, 
       `map`        VARCHAR(255) DEFAULT NULL, 
       `key_key`    VARCHAR(255) NOT NULL, 
       PRIMARY KEY (`example_id`, `key_key`), 
       KEY `fkea50c74c5cd44e54` (`example_id`), 
       CONSTRAINT `fkea50c74c5cd44e54` FOREIGN KEY (`example_id`) REFERENCES 
       `hash` (`id`) 
    ) 
  engine=innodb 
  DEFAULT charset=utf8
  </code></pre>
  
  <p>hash table description :</p>
  
  <pre><code>CREATE TABLE `hash` (
    `id` int(11) NOT NULL,
    PRIMARY KEY (`id`)
  ) ENGINE=InnoDB DEFAULT CHARSET=utf8
  </code></pre>
  
  <p>Why am I unable to describe the fields map and key_key as  hashmap ?I want them to be in a single field like key value pairs. can anyone please help me ?</p>