title: Hibernate does not throw an exception on duplicate primary key
body: <p>I am working with Spring Data JPA. Given this setup:</p>
  
  <pre><code>@Entity
  public class Role
  {
      @Id
      @Column(nullable = false, unique = true)
      private String name;
  
      @Column
      private String info;
  }
  
  @Repository
  public interface RoleRepository extends CrudRepository&lt;Role, String&gt;
  {
  }
  </code></pre>
  
  <p>I can simply insert a <code>Role</code> with the same primary key (<code>name</code>) twice. It justs updates the entry in the database. I would like it to throw an exception though. Trying to insert two entries with the same primary key at database-level gives the desired "duplicate key"-error.</p>
  
  <hr/>
  
  <h1>Example:</h1>
  
  <p>Role table is empty</p>
  
  <pre><code>MariaDB [logz]&gt; select * from role;
  Empty set (0.00 sec)
  </code></pre>
  
  <p>Saving the first time</p>
  
  <pre><code>roleRepository.save(new Role("name1", "info0"));
  </code></pre>
  
  <p>Hibernate first checking, then saving</p>
  
  <pre><code>Hibernate: select role0_.name as name1_3_0_, role0_.info as info2_3_0_ from role role0_ where role0_.name=?
  Hibernate: insert into role (info, name) values (?, ?)
  </code></pre>
  
  <p>Role table is filled</p>
  
  <pre><code>MariaDB [logz]&gt; select * from role;
  +-------+-------+
  | name  | info  |
  +-------+-------+
  | name1 | info0 |
  +-------+-------+
  1 row in set (0.00 sec)
  </code></pre>
  
  <p>Saving the second time</p>
  
  <pre><code>roleRepository.save(new Role("name1", "info1"));
  </code></pre>
  
  <p>Hibernate checking and updating - I want an exception here though!</p>
  
  <pre><code>Hibernate: select role0_.name as name1_3_0_, role0_.info as info2_3_0_ from role role0_ where role0_.name=?
  Hibernate: update role set info=? where name=?
  </code></pre>
  
  <p>Role table is simply updated :(</p>
  
  <pre><code>MariaDB [logz]&gt; select * from role;
  +-------+-------+
  | name  | info  |
  +-------+-------+
  | name1 | info1 |
  +-------+-------+
  1 row in set (0.00 sec)
  </code></pre>