title: Persisting a JSON Object using Hibernate and JPA
body: <p>I am trying to store a JSON object in MySQL database in spring boot. I know I am doing something wrong but I a can't figure out what it is because I am fairly new to Spring. </p>
  
  <p>I have a rest endpoint where I get the following JSON object (via HTTP PUT) and I need to store it in database so that the user can fetch it later (via HTTP GET). </p>
  
  <pre><code>{
    "A": {
      "Name": "Cat",
      "Age": "1"
    },
    "B": {
      "Name": "Dog",
      "Age": "2"
    },
    "C": {
      "Name": "Horse",
      "Age": "1"
    }
  }
  </code></pre>
  
  <p>Note that in the above case The <strong>number</strong> <strong>of</strong> <strong>keys</strong> in the object may vary, Due to that requirement I am using a <code>HashMap</code> to catch the object in the controller. </p>
  
  <pre><code>@RequestMapping(method = RequestMethod.POST)
      public String addPostCollection(@RequestBody HashMap&lt;String, Animal&gt; hp) {
  
          hp.forEach((x, y) -&gt; {
              postRepository.save(hp.get(x));
          });
  
          return "OK";
  
      }
  </code></pre>
  
  <p>As you can see in the method, I can iterate the <code>HashMap</code> and persist each <code>Animal</code> object in db. But I am looking for a way to persist the entire <code>HashMap</code> in a single record. I have did some reading and they suggest me to use a <code>@ManyToMany</code> mapping. </p>
  
  <p>Can anyone point me in a direction to persist the <code>HashMap</code> in a different way? (or is using the <code>@ManyToMany</code> the only and right way to do this?) </p>