title: POSTing a @OneToMany sub-resource association in Spring Data REST
body: <p>Currently I have a Spring Boot application using Spring Data REST. I have a domain entity <code>Post</code> which has the <code>@OneToMany</code> relationship to another domain entity, <code>Comment</code>. These classes are structured as follows:</p>
  
  <p>Post.java:</p>
  
  <pre><code>@Entity
  public class Post {
  
      @Id
      @GeneratedValue
      private long id;
      private String author;
      private String content;
      private String title;
  
      @OneToMany
      private List&lt;Comment&gt; comments;
  
      // Standard getters and setters...
  }
  </code></pre>
  
  <p>Comment.java:</p>
  
  <pre><code>@Entity
  public class Comment {
  
      @Id
      @GeneratedValue
      private long id;
      private String author;
      private String content;
  
      @ManyToOne
      private Post post;
  
      // Standard getters and setters...
  }
  </code></pre>
  
  <p>Their Spring Data REST JPA repositories are basic implementations of <code>CrudRepository</code>:</p>
  
  <p>PostRepository.java:</p>
  
  <pre><code>public interface PostRepository extends CrudRepository&lt;Post, Long&gt; { }
  </code></pre>
  
  <p>CommentRepository.java:</p>
  
  <pre><code>public interface CommentRepository extends CrudRepository&lt;Comment, Long&gt; { }
  </code></pre>
  
  <p>The application entry point is a standard, simple Spring Boot application. Everything is configured stock.</p>
  
  <p>Application.java</p>
  
  <pre><code>@Configuration
  @EnableJpaRepositories
  @Import(RepositoryRestMvcConfiguration.class)
  @EnableAutoConfiguration
  public class Application {
  
      public static void main(final String[] args) {
          SpringApplication.run(Application.class, args);
      }
  }
  </code></pre>
  
  <p>Everything appears to work correctly. When I run the application, everything appears to work correctly. I can POST a new Post object to <code>http://localhost:8080/posts</code> like so:</p>
  
  <p>Body: 
  <code>{"author":"testAuthor", "title":"test", "content":"hello world"}</code></p>
  
  <p>Result at <code>http://localhost:8080/posts/1</code>:</p>
  
  <pre><code>{
      "author": "testAuthor",
      "content": "hello world",
      "title": "test",
      "_links": {
          "self": {
              "href": "http://localhost:8080/posts/1"
          },
          "comments": {
              "href": "http://localhost:8080/posts/1/comments"
          }
      }
  }
  </code></pre>
  
  <p>However, when I perform a GET at <code>http://localhost:8080/posts/1/comments</code> I get an empty object <code>{}</code> returned,  and if I try to POST a comment to the same URI, I get an HTTP 405 Method Not Allowed.</p>
  
  <p>What is the correct way to create a <code>Comment</code> resource and associate it with this <code>Post</code>? I'd like to avoid POSTing directly to <code>http://localhost:8080/comments</code> if possible.</p>