title: Hibernate Delete entity from manyTomany releationship
body: <p>In my database a post can be in many categories and a category may have many posts. I have one relation for post and another for category. Here is their JPA mappings:</p>
  
  <p>Post entity:</p>
  
  <pre><code>@ManyToMany(cascade = {PERSIST, MERGE})
  private Set&lt;Category&gt; categories = new HashSet&lt;&gt;();
  </code></pre>
  
  <p>Category entity:</p>
  
  <pre><code>@ManyToMany(mappedBy = "categories")
  private Set&lt;Post&gt; posts = new HashSet&lt;&gt;();
  </code></pre>
  
  <p>Other operations work properly but when I try to delete a post: </p>
  
  <pre><code>@CacheEvict(cacheNames = "pinnedPosts", allEntries = true)
  public void deletePost(long postId) {
      Optional&lt;Post&gt; optionalPost = postRepository.findById(postId);
      if (optionalPost.isPresent()) {
          Post post = optionalPost.get();
          for (Category category : post.getCategories()) {
              long newScore = post.getAuthor().getScore() - post.getLikesCount() * likeScore;
              post.getAuthor().setScore(newScore);
              category.getPosts().remove(post);
          }
          postRepository.deleteById(postId);
      } else {
          throw new PostNotFoundException();
      }
  }
  </code></pre>
  
  <p><strong>deletion succeeds</strong> but hibernate throws exception (I want it to be deleted without any exception!). here is hibernate queries and the exception that hibernate throws (I don't know why queries are executed twice. Does this causes the problem?):</p>
  
  <pre><code>...
  Hibernate: delete from post_categories where posts_id=?
  Hibernate: delete from post_categories where posts_id=?
  Hibernate: delete from post_likers where favorites_id=?
  Hibernate: delete from post_likers where favorites_id=?
  Hibernate: delete from post where id=?
  Hibernate: delete from post where id=?
  
  2018-05-12 20:59:53.621 ERROR 14652 --- [nio-8000-exec-4] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.orm.ObjectOptimisticLockingFailureException: Batch update returned unexpected row count from update [0]; actual row count: 0; expected: 1; nested exception is org.hibernate.StaleStateException: Batch update returned unexpected row count from update [0]; actual row count: 0; expected: 1] with root cause
  
  org.hibernate.StaleStateException: Batch update returned unexpected row count from update [0]; actual row count: 0; expected: 1
  at org.hibernate.jdbc.Expectations$BasicExpectation.checkBatched(Expectations.java:67) ~[hibernate-core-5.2.14.Final.jar:5.2.14.Final]
  at org.hibernate.jdbc.Expectations$BasicExpectation.verifyOutcome(Expectations.java:54) ~[hibernate-core-5.2.14.Final.jar:5.2.14.Final]
  at org.hibernate.engine.jdbc.batch.internal.NonBatchingBatch.addToBatch(NonBatchingBatch.java:46) ~[hibernate-core-5.2.14.Final.jar:5.2.14.Final]
  at org.hibernate.persister.entity.AbstractEntityPersister.delete(AbstractEntityPersister.java:3325) ~[hibernate-core-5.2.14.Final.jar:5.2.14.Final]
  at org.hibernate.persister.entity.AbstractEntityPersister.delete(AbstractEntityPersister.java:3562) ~[hibernate-core-5.2.14.Final.jar:5.2.14.Final]
  at org.hibernate.action.internal.EntityDeleteAction.execute(EntityDeleteAction.java:99) ~[hibernate-core-5.2.14.Final.jar:5.2.14.Final]
  at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:599) ~[hibernate-core-5.2.14.Final.jar:5.2.14.Final]
  at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:473) ~[hibernate-core-5.2.14.Final.jar:5.2.14.Final]
  at org.hibernate.event.internal.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:337) ~[hibernate-core-5.2.14.Final.jar:5.2.14.Final]
  ...
  </code></pre>