views:

505

answers:

3

Hi,

Is there a way to find out which record caused such a violation in Hibernate?

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.

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.

Thanks.


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)
+1  A: 

I would implement one or more Hibernate Event Listeners and do a bit of logging before performing operations. Hopefully this is suitable for your scenario.

(I suspect you are not using MySQL as I am, because MySQL actually returns the offending unique key if a constraint is violated)

_NT
We already have auditing interceptors but when the list is big then its not easy to track down which of the element caused the error. BTW we are using Oracle.
lud0h
Perhaps this post will come in use:http://technicalpickles.com/posts/tracking-down-oracle-constraint-violations-with-a-little-sql-and-toad/
_NT
A: 

I simply set a breakpoint in AbstractEntityPersister::performInsert(...) to find out the offending record.

As _NT said may be MySQL supports this but not Oracle.

But will keep this open to know any elegant solution.

lud0h
Why aren't event listeners elegant?
_NT
As I explained in my above comment, until I try to 'save' I will not about the constraint and then its too late. I was looking for something like you said about 'MySQL', may be its Oracle the culprit :-)
lud0h
A: 

Using TRACE level log level displays the values of all parameters that are passed into the database.

SDev