title: Defining JPA simple relationship
body: <p>Iam a little bit confused by the JPA relationship definitions for my case and could use some help to make it correct. Even if it seems so easy, I run in diffrent errors all the time.</p>
  
  <p>examplary:</p>
  
  <pre><code>@Entity
  public class Person 
  {
    ...
    @ManyToMany(cascade = {CascadeType.PERSIST, CascadeType.MERGE}) // cascade what?
    private List&lt;Attribute&gt; attribs;     
  }
  
  @Entity
  public class Attribute 
  {
    @Id
    private Integer id;
    ...
  }
  </code></pre>
  
  <p>All Persons can have same Attribute and so on.</p>
  
  <p>The point is that Person should be able to cascade Persist/merge Attribute without generating duplicates.</p>
  
  <p>As I said, its seems so easy, I just got stuck cause diffrent Exceptions I meet. And the right direction to "fix" the right config :)</p>
  
  <p>Thanks for any help.</p>
  
  <p><strong>EDIT:</strong></p>
  
  <p>OK, its seems that its a ManyToMany relationship. Even if One Person can have an Attribute only once? But a diffrent Person can have that too.
  But now I have this cascading persist/merge problem. </p>
  
  <p>eg:</p>
  
  <pre><code>Person a = new Person();
  a.add(new Attribute(1));
  a.add(new Attribute(2));
  
  repo.save(a); //worked and persists Attributes
  
  Person b = new Person();
  a.add(new Attribute(1));
  a.add(new Attribute(2));
  a.add(new Attribute(3));
  
  repo.save(b); //Error: IllegalStateException: Multiple representations of the same entity ..Attribute..  are being merged. Detached ..Attribute..
  </code></pre>
  
  <p>Same happens if just want to add a new Attribute to Person a. On which I have to send the whole list of existing Attributes additionally to the new one.</p>