POST method with a @manytoone relation in Spring REST controller
<p>This is my very first attempt at writing a REST service that has entities with bi-directional <code>ManyToOne</code> relations. The code is below, and I have encountered the following problem:</p>

<p>When I run the POST request to /posts/ it creates a new category every time, treating the category part in JSON as the name of the new category.</p>

<p><strong>Post.java</strong></p>

<pre><code>@Entity
public class Post {
    @Column(name = "post_id")
    private Long id;

    @Column(nullable = false, length = 255)
    private String title;

    @Column(nullable = false, length = 999999)
    private String text;

    private Category category;

    // Constructors
    public Post() {
    }

    public Post(String title, String text, Category category) {
        this.title = title;
        this.text = text;
        this.category = category;
    }

    // Getters and Setters


    @Id @GeneratedValue(strategy = GenerationType.IDENTITY)
    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getText() {
        return text;
    }

    public void setText(String text) {
        this.text = text;
    }

    @ManyToOne(fetch = FetchType.EAGER, cascade = CascadeType.PERSIST)
    @JoinColumn(name = "category_id")
    public Category getCategory() {
        return category;
    }

    public void setCategory(Category category) {
        this.category = category;
    }
}
</code></pre>

<p><strong>Category.java</strong></p>

<pre><code>@Entity
public class Category {
    @Column(name = "category_id")
    private Long id;

    @Column(nullable = false)
    private String name;

    @Column(nullable = false)
    private String slug;

    private List&lt;Post&gt; posts;

    // Constructors

    public Category() {
    }

    public Category(String name, String slug) {
        this.name = name;
        this.slug = slug;
    }

    public Category(String name) {
        this.name = name;
    }

    // Adding posts to existing categories

    public void addPost(Post post) {
        this.posts.add(post);
    }

    // Getters and setters

    @Id @GeneratedValue(strategy = GenerationType.IDENTITY)
    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getSlug() {
        return slug;
    }

    public void setSlug(String slug) {
        this.slug = slug;
    }

    @JsonIgnore @OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL, mappedBy = "category")
    public List&lt;Post&gt; getPosts() {
        return posts;
    }

    public void setPosts(List&lt;Post&gt; posts) {
        this.posts = posts;
    }

    // toString


    @Override
    public String toString() {
        final StringBuffer sb = new StringBuffer("Category{");
        sb.append("id=").append(id);
        sb.append(", name='").append(name).append('\'');
        sb.append(", slug='").append(slug).append('\'');
        sb.append(", posts=").append(posts);
        sb.append('}');
        return sb.toString();
    }
}
</code></pre>

<p><strong>PostController.java</strong></p>

<pre><code>@RequestMapping(value = "/posts")
@RestController
public class PostController {

    @Autowired
    PostRepository repository;

    @RequestMapping(method = RequestMethod.GET)
    public @ResponseBody List&lt;Post&gt; getAllPosts() {
        return repository.findAll();
    }

    @RequestMapping(method = RequestMethod.POST)
    @ResponseStatus(HttpStatus.CREATED) public
    @ResponseBody Post createPost(@RequestBody Post post) {
        return repository.save(post);
    }
}
</code></pre>