title: Spring Boot - How to create entities and nested entities in single POST request
body: <p>I’m trying to figure out specific thing with Spring Data, and that is: how to  create an entity and a related entity with one single POST request.</p>
  
  <p>I can create related entities with separate requests, but with single request I’m not sure. (without creating a specific controller method to it). Example:</p>
  
  <p><strong>ReusableComponent.java</strong></p>
  
  <pre><code>@Entity
  public class ReusableComponent {
         @Id
         @GeneratedValue(strategy=GenerationType.IDENTITY)
         private Long id;
  
         @OneToMany(mappedBy="component", cascade = CascadeType.ALL)
         private List&lt;Consumer&gt; consumers = new ArrayList&lt;&gt;();
  
         private String name;
  
         public Long getId() {
                return id;
         }
         public void setId(Long id) {
                this.id = id;
         }
         public List&lt;Consumer&gt; getConsumers() {
                return consumers;
         }
         public void setConsumers(List&lt;Consumer&gt; consumers) {
                this.consumers = consumers;
         }
         public String getName() {
                return name;
         }
         public void setName(String name) {
                this.name = name;
         }
  }
  </code></pre>
  
  <p><strong>Consumer.java</strong></p>
  
  <pre><code>@Entity
  public class Consumer&lt;ResuableComponent&gt; {
         @Id
         @GeneratedValue(strategy=GenerationType.IDENTITY)
         private Long id;
  
      private String name;
  
         @ManyToOne(targetEntity=ReusableComponent.class)
         private ResuableComponent component;
  
         public Long getId() {
                return id;
         }
         public void setId(Long id) {
                this.id = id;
         }
         public ResuableComponent getComponent() {
                return component;
         }
         public void setComponent(ResuableComponent component) {
                this.component = component;
         }
         public String getName() {
                return name;
         }
         public void setName(String name) {
                this.name = name;
         }
  }
  </code></pre>
  
  <p><strong>ReusableComponentRepository.java</strong></p>
  
  <pre><code>public interface ReusableComponentRepository extends CrudRepository&lt;ReusableComponent, Long&gt; 
  {
         Iterable&lt;ReusableComponent&gt; findAll();
  
         @RestResource(exported = false)
         List&lt;ReusableComponent&gt; findByName(String name);
  }
  </code></pre>
  
  <p><strong>POST  <a href="http://localhost:8080/reusableComponents" rel="nofollow noreferrer">http://localhost:8080/reusableComponents</a></strong></p>
  
  <p>Request Body:</p>
  
  <pre><code>{ "name": "datagrid", "consumers": [{ "name": "financial app" }] }
  </code></pre>
  
  <p><strong>GET  <a href="http://localhost:8080/reusableComponents" rel="nofollow noreferrer">http://localhost:8080/reusableComponents</a></strong></p>
  
  <p>Response:</p>
  
  <pre><code>{
      "_embedded": {
          "reusableComponents": [
              {
                  "consumers": [],
                  "name": null,
                  "_links": {
                      "self": {
                          "href": "http://localhost:8080/reusableComponents/1"
                      },
                      "reusableComponent": {
                          "href": "http://localhost:8080/reusableComponents/1"
                      }
                  }
              }
          ]
      },
      "_links": {
          "self": {
              "href": "http://localhost:8080/reusableComponents"
          },
          "profile": {
              "href": "http://localhost:8080/profile/reusableComponents"
          }
      }
  }
  </code></pre>
  
  <p>As you can see there are no consumers in the reusableComponent instance.  I had this working before, but for the life of me I can’t figure it out now.</p>