I'm using Hibernate 4.0.1.Final. I have a table with a unique constraint column …
@Entity
@Table(name = "cb_contract",
uniqueConstraints = {@UniqueConstraint(columnNames={"ORDER_ID"})}
)
public class Contract {
...
@Column(name = "ORDER_ID")
private String orderId;
Currently, if I try and save two objects that have the same value for the order ID column, using the below method ...
protected void saveOrUpdate(Object obj) {
final Session session = sessionFactory.getCurrentSession();
session.saveOrUpdate(obj);
}
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?