How do I get the data that caused the unique constraint violation (via Hibernate)?

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

Typically you add objects to the session and at the end you save them. If this error occurs, it takes some time to track which entry violated the constraint.

Is there a way to find out which record is called (either tostring () in the case of new objects, or Primary Key in the case of existing objects, should greatly simplify the debugging process.

Thank.


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)

      

+2


source to share


3 answers


I just set a breakpoint at AbstractEntityPersister::performInsert(...)

to find out the offending entry.

As _NT says, MySQL can support this, but not Oracle.



But he will keep it open to know any elegant solution.

0


source


I would do one or more Hibernate Event Listeners and do a little registration before doing the operations. Hope this works for your scenario.



(I suspect you are not using MySQL the way I do, because MySQL actually returns an offensive unique key if the constraint is violated)

+1


source


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

0


source







All Articles