title: Spring boot REST api, pass id on foreign key instead of entire object
body: <p>I'm trying to build a REST API with Spring boot application for web and mobile app. In my application I have multiple foreign keys, and I want to pass only the id of the object instead of the entire object. See example below.</p>
  
  <p><strong>Model Book</strong></p>
  
  <pre><code>// imports
  
  @Entity
  public class Book {
  
      @Id
      @GeneratedValue(strategy = GenerationType.IDENTITY)
      private Integer id;
  
      private String title;
      private String isbn;
  
      @ManyToOne
      @JoinColumn(name = "category_id", updatable = false)
      private BookCategory bookCategory;
  
      public Book() {
      }
  
      public Book(Integer id, String title, String isbn, BookCategory bookCategory) {
          this.id = id;
          this.title = title;
          this.isbn = isbn;
          this.bookCategory = bookCategory;
      }
  
      // getters and setters
  }
  </code></pre>
  
  <p><strong>Model Category</strong></p>
  
  <pre><code>// imports
  
  @Entity
  public class BookCategory {
  
      @Id
      @GeneratedValue(strategy = GenerationType.IDENTITY)
      private Integer id;
  
      private String name;
  
      @OneToMany(mappedBy = "bookCategory", cascade = CascadeType.ALL)
      private List&lt;Book&gt; books;
  
      public BookCategory() {
      }
  
      public BookCategory(Integer id, String name) {
          this.id = id;
          this.name = name;
      }
  
      // getters and setters
  }
  </code></pre>
  
  <p><strong>Book Controller</strong></p>
  
  <pre><code>// imports
  
  @RestController
  @RequestMapping(path = "/api/v1")
  public class BookController {
  
      @Autowired
      private BookRepository bookRepository;
  
      // other methods for index, show, put and delete
  
      @PostMapping(path = "/books")
      public book createBook(@RequestBody Book book) {
          return bookRepository.save(book);
      }
  
  }
  </code></pre>
  
  <p><strong>Book Repository</strong></p>
  
  <pre><code>// imports
  
  public interface BookRepository extends PagingAndSortingRepository&lt;Book, Integer&gt; {
  }
  </code></pre>
  
  <p>Book categories are pre-loaded on database, and I don't want to allow the client to change them or to add new ones.</p>
  
  <p>Here's the JSON POST I tried and the object that was stored on database.</p>
  
  <p>Request</p>
  
  <pre><code>{
      "title": "Tirant lo blanc",
      "isbn": "9788475841199",
      "category_id": 4
  }
  </code></pre>
  
  <p>Response</p>
  
  <pre><code>{
      "title": "Tirant lo blanc",
      "isbn": "9788475841199",
      "bookCategory": null
  }
  </code></pre>
  
  <p>I tried to send it as object too, but the result is null.</p>
  
  <pre><code>{
      "title": "Tirant lo blanc",
      "isbn": "9788475841199",
      "category_id": {
          "id": 4
      }
  }
  </code></pre>
  
  <p>How can I send the category id to correctly store the book on database? Is better to send the entire category object instead? I tried too, but the response and the stored book on database is always on category null.</p>