title: How to retrieve data which caused unique constraint violation (via Hibernate)?
body: <p>Is there a way to find out which record caused such a violation in Hibernate?</p>
  
  <p>Normally you add objects to session and at the end you persist them. If such an error occurs it takes a while to track down which record has violated the constraint. </p>
  
  <p>Is there way to find out which record caused (either to "toString() in case of new objects or Primary Key in case of existing objects should simplify debug process enormously.</p>
  
  <p>Thanks.</p>
  
  <hr>
  
  <pre><code>org.hibernate.exception.ConstraintViolationException: could not insert: [com.project.valueobject.mapping.Model]
      at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:71)
      at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
      at org.hibernate.id.insert.AbstractReturningDelegate.performInsert(AbstractReturningDelegate.java:40)
      at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2163)
      at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2643)
      at org.hibernate.action.EntityIdentityInsertAction.execute(EntityIdentityInsertAction.java:51)
      at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:279)
      at org.hibernate.event.def.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:298)
      at org.hibernate.event.def.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:181)
      at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:107)
      at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:187)
      at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:172)
      at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.performSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:94)
      at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:70)
      at org.hibernate.impl.SessionImpl.fireSaveOrUpdate(SessionImpl.java:507)
      at org.hibernate.impl.SessionImpl.saveOrUpdate(SessionImpl.java:499)
      at org.hibernate.impl.SessionImpl.saveOrUpdate(SessionImpl.java:495)
  </code></pre>