title: How to setup a basic manytomany relationship with JPA/Hibernate?
body: <p>I have a very basic many to many relationship: Persons and Projects, set up as follows:</p>
  
  <p>Person:</p>
  
  <pre><code>@ManyToMany(cascade=CascadeType.ALL)
  @JoinTable(name = "person_project", 
  joinColumns = @JoinColumn(name = "Person_Id"),
  inverseJoinColumns = @JoinColumn(name = "Project_id"))
  private Set&lt;Project&gt; projects = new HashSet&lt;Project&gt;();
  </code></pre>
  
  <p>Project:</p>
  
  <pre><code>@ManyToMany
  @JoinTable(name = "person_project", 
  joinColumns = @JoinColumn(name = "Project_Id"), 
  inverseJoinColumns = @JoinColumn(name = "Person_id"))
  private Set&lt;Person&gt; persons = new HashSet&lt;Person&gt;();
  </code></pre>
  
  <p>When updating a Person, new projects may be specified. I have a JPARepository set up for Person and I use saveAndFlush(...) to save the possible new Projects with the Person.</p>
  
  <p>This works for one Person. If I add a new Project it will be saved with the Person and the tables in the database update correctly. However, If I then add the same Project for another Person - it will be deleted from the first Person! The id of the first person is replaced with the id of the second person in the join table.</p>
  
  <p>What causes this? is my relation setup incorrect?</p>