title: Spring Data Rest/JPA cascade persist on patch
body: <p>I have two entities, <code>Book</code> and <code>BookEvent</code>. They are linked by a ManyToOne-Relationship, i.e. one Book can have many BookEvents.
  I view Book as the aggregate root and have one Repository for the Book entity, but not for the BookEvent.</p>
  
  <pre><code>@Entity
  @Table(name = "book")
  public class Book {
  
      @Id
      @GeneratedValue(strategy = GenerationType.IDENTITY)
      private Long id;
  
      private String title;
  
      @OneToMany(mappedBy = "book", cascade = CascadeType.ALL, orphanRemoval = true)
      private List&lt;BookEvent&gt; events = new ArrayList&lt;&gt;();
  
  
      public List&lt;BookEvent&gt; getEvents() {
          return events;
      }
  
      public void setEvents(List&lt;BookEvent&gt; events) {
          for (BookEvent event: events) {
              event.setBook(this);
          }
          this.events = events;
      }
  
      public void addEvent(BookEvent event) {
          events.add(event);
          event.setBook(this);
      }
  
      public void removeEvent(BookEvent event) {
          events.remove(event);
          event.setBook(null);
      }
  }
  </code></pre>
  
  <p>(Other getters/setters are omitted here).</p>
  
  <pre><code>@Entity
  public class BookEvent {
  
      @Id @GeneratedValue(strategy = GenerationType.IDENTITY)
      private long id;
  
      @ManyToOne
      @JoinColumn(name="book_id")
      private Book book;
      private LocalDate date;
  
  
      @PreRemove
      private void removeEventFromBook(){
          book.removeEvent(this);
      }
  
      public Book getBook() {
          return book;
      }
  
      public void setBook(Book book) {
          this.book = book;
          if (!this.book.getEvents().contains(this)) {
              this.book.getEvents().add(this);
          }
      }
  }
  </code></pre>
  
  <p>I now want to add a new event to the book after the book has been created. I use Spring Data Rest.</p>
  
  <p>Creating the book with a <code>POST</code> and one event works fine:</p>
  
  <pre><code>{
      "title": "My example book",
      "events": [ 
          {
              "type": "BOUGHT",
              "date": "2017-05-09"
          }
          ]
  }
  </code></pre>
  
  <p>Gives the answer:</p>
  
  <pre><code>{
      "title": "My example book",
      "events": [
          {
              "id": 3,
              "date": "2017-05-09",
              "_links": {
                  "book": {
                      "href": "http://localhost:8080/api/books/2"
                  }
              }
          }
      ]
  }
  </code></pre>
  
  <p>But if I then do a JSON Patch to append one new event, the event is included in the response to the PATCH request, but it is actually not saved in the database (a GET on the book afterwards does not return the event and when the database the column book_id is null).</p>
  
  <pre><code>[
      {
      "op": "add",
      "path": "/events/-",
      "value": 
          {
              "date": "2017-05-09"
          }
  }
  ]
  </code></pre>
  
  <p>When using the debugger, the <code>setEvents()</code> method is called on the initial POST request, but during the PATCH request, only the <code>getEvents()</code> method is called - no <code>setBook()</code> or <code>addEvent()</code> method. I think the problem is there.</p>
  
  <p>Do I have a problem with my entity setup?</p>