title: Cascade insert in hibernate and unique constraint on cascading table
body: <p>I have a following SQL schema layout:</p>
  
  <pre><code>-- postgresql82+ syntax
  
  create table AudioTracks (
    id serial primary key 
  , name text
  , size integer
  , filePath text
  , additionDate timestamp default now()
  );
  
  create table Genres (
    id serial primary key
  , name text unique -- here is the unique constraint which troubles me 
  , description text
  , additionDate timestamp default now()
  );
  
  create table AudioTrackGenre (
    genreId integer references Genres (id) unique 
  , audioTrackId integer references AudioTracks (id) unique 
  , additionDate timestamp default now()
  );
  </code></pre>
  
  <p>And two corresponding mappings to tables:</p>
  
  <pre><code>@Entity(name = "AudioTracks")
  public class AudioTrack implements Serializable {
      @Id
      @Column
      @GeneratedValue(strategy = GenerationType.IDENTITY)
      private Integer id;
  
      @Column
      private String name;
  
      @Column
      private Integer size;
  
      @Column
      private String filePath;
  
      @Column
      @Temporal(TemporalType.TIMESTAMP)
      private Date additionDate;
  
  
      @ManyToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL )
      @JoinTable(name = "AudioTrackGenre",
              joinColumns =  { @JoinColumn(name = "audioTrackId") },
              inverseJoinColumns = { @JoinColumn(name = "genreId") }
      )
      @OrderBy("name")
      private Set&lt;Genre&gt; genres = new HashSet&lt;Genre&gt;();
  
      // setter/getter methods //
      ....
  }
  </code></pre>
  
  <p>and</p>
  
  <pre><code>@Entity(name = "Genres")
  public class Genre implements Serializable {
      @Id
      @Column
      @GeneratedValue(strategy = GenerationType.IDENTITY)
      private Integer id;
  
      @Column
      private String name;
  
      @Column
      private String description;
  
      @Column
      @Temporal(TemporalType.TIMESTAMP)
      private Date additionDate;
  
      @ManyToMany(mappedBy = "genres")
      private Set&lt;AudioTrack&gt; audioTracks = new HashSet&lt;AudioTrack&gt;();
  
      public Genre() { }
      public Genre(String name) { this.name = name; }
  
      // setter/getter methods //
      ....
  }
  </code></pre>
  
  <p>But whenever i am trying to save AudioTrack, populated with Genres which are already exists in Genres table, like here:</p>
  
  <pre><code>        Set&lt;Genre&gt; genres = new HashSet&lt;Genre&gt;();
          genres.add(new Genre("ambient"));
  
          AudioTrack track = new AudioTrack();
          track.setGenres(genres);
  
          audioTrackService.addAudioTrack(track);
  </code></pre>
  
  <p>(the <code>audioTrackService.addAudioTrack(track)</code> thing does <code>sessionFactory.getCurrentSession().save(track)</code> at lowest DAO level)</p>
  
  <p>i am getting:</p>
  
  <pre><code>ERROR: ERROR: duplicate key value violates unique constraint "genres_name_key"
    Detail: Key (name)=(ambient) already exists.
  </code></pre>
  
  <p>How do i tell Hibernate not to try to re-insert already existing genres to <code>Genres</code> table on cascade inserts?</p>