title: Unique contraint violation on OneToMany relationship
body: <p>I have a <code>City</code> entity and a <code>Person</code> entity. I want to setup a <code>OneToMany</code> relationship from Person to City indicating which cities a person has lived in. Its a uni-directional OneToMany relationship from Person.</p>
  
  <p>My City class looks like this</p>
  
  <pre><code>@Entity
  public class City {
  
    @Id
    @GeneratedValue
    private Long id;
  
    private String name;
    ...
  }
  </code></pre>
  
  <p><a href="https://github.com/adeelmahmood/jpatest-onetomany/blob/master/src/main/java/jpatest/City.java" rel="nofollow">link to City class code</a></p>
  
  <p>The person entity looks like this</p>
  
  <pre><code>@Entity
  public class Person {
  
     @Id
     @GeneratedValue
     private Long id;
  
     private String name;
  
     @OneToMany(fetch = FetchType.EAGER, orphanRemoval = true)
     private Set&lt;City&gt; livedInCities = new HashSet&lt;City&gt;();
     ...
  }
  </code></pre>
  
  <p><a href="https://github.com/adeelmahmood/jpatest-onetomany/blob/master/src/main/java/jpatest/Person.java#L21" rel="nofollow">link to Person class code</a></p>
  
  <p>In my test I am simply creating a city first and saving it. Then creating a person and adding the saved city instance on it before saving that person. After saving the person, everything is fine. </p>
  
  <p>When I add a second person and add the same city on the second person, it throws a unique constraint violation. I am not sure why this is happening because in the relationship table, this should be inserted as a new row for person 2 and city 1.
  Test code</p>
  
  <pre><code>@Test
  public void test() {
  
      City chicago = new City();
      chicago.setName("chicago");
      City savedChicago = cityRepo.save(chicago);
  
      Person john = new Person();
      john.setName("john");
      john.addCity(savedChicago);
      Person savedJohn = personRepo.save(john);
  
      Person tom = new Person();
      tom.setName("tom");
      tom.addCity(savedChicago);
      Person savedTom = personRepo.save(tom);
  
      System.out.println(savedTom);
      System.out.println(savedJohn);
  
  }
  </code></pre>
  
  <p><a href="https://github.com/adeelmahmood/jpatest-onetomany/blob/master/src/test/java/jpatest/RepositoryTests.java" rel="nofollow">link to Test code</a></p>
  
  <blockquote>
    <p>2015-07-02 18:11:21.026  WARN 5473 --- [           main] o.h.engine.jdbc.spi.SqlExceptionHelper   : SQL Error: 23505, SQLState: 23505
    2015-07-02 18:11:21.026 ERROR 5473 --- [           main] o.h.engine.jdbc.spi.SqlExceptionHelper   : Unique index or primary key violation: "UK_B6XKGKBT91IXB0WE6N030RFPQ_INDEX_C ON PUBLIC.PERSON_LIVED_IN_CITIES(LIVED_IN_CITIES_ID) VALUES (1, 1)"; SQL statement:</p>
  </blockquote>
  
  <p>The complete project for this is available at 
  <a href="https://github.com/adeelmahmood/jpatest-onetomany" rel="nofollow">https://github.com/adeelmahmood/jpatest-onetomany</a></p>
  
  <p>I would appreciate any help.</p>