title: Can I make Hibernate replace an object based off a unique column constraint?
body: <p>I'm using Hibernate 4.0.1.Final.  I have a table with a unique constraint column …</p>
  
  <pre><code>@Entity
  @Table(name = "cb_contract",
      uniqueConstraints = {@UniqueConstraint(columnNames={"ORDER_ID"})}
  )
  public class Contract {
      ... 
      @Column(name = "ORDER_ID")
      private String orderId;
  </code></pre>
  
  <p>Currently, if I try and save two objects that have the same value for the order ID column, using the below method ...</p>
  
  <pre><code>protected void saveOrUpdate(Object obj) {
      final Session session = sessionFactory.getCurrentSession();
      session.saveOrUpdate(obj);
  }
  </code></pre>
  
  <p>I get a "org.hibernate.exception.ConstraintViolationException: integrity constraint violation: unique constraint or index violation" exception when saving the second instance.  Is there a way I can make Hibernate save or replace objects based off the unique column or do I need to search for the object first, delete it if it is there, and then re-insert the one?</p>