Subject | Hibernate and database constraints |
---|---|
Author | Christian Stengel |
Post date | 2007-09-12T14:08:07Z |
Hi *,
I am using hibernate to insert data within a firebird 2.0 classic
server. One of my tables has a unique key constraint defined on a
varchar field.
How can I catch this constraint using hibernate and extract a
meaningfull error message? It seems that the hibernates
ConstraintViolationException is not thrown - or used by hibernate in
conjunction with firebird (it gets thrown when using mysql).
I can catch the SQLErrorCode - but it binds me to firebird - it would
be nicer if there is a generic way to handle such behaviour using
build in hibernate exceptions.
The SQLErrorCode is 335544665 but the SQLState is mostly HY000. It
seems, that hibernate uses the first two characters of the sqlstate
to determine the error cathegory (defined in SQLStateConverter.java)
- and thus throws a more specific exception than the
GenericJDBCException exception.
Relevant parts of the stacktrace look like:
org.hibernate.exception.GenericJDBCException: could not insert:
[my.project.model.Partner]
at
org.hibernate.exception.SQLStateConverter.handledNonSpecificException
(SQLStateConverter.java:103)
at org.hibernate.exception.SQLStateConverter.convert
(SQLStateConverter.java:91)
at org.hibernate.exception.JDBCExceptionHelper.convert
(JDBCExceptionHelper.java:43)
at org.hibernate.persister.entity.AbstractEntityPersister.insert
(AbstractEntityPersister.java:2263)
at org.hibernate.persister.entity.AbstractEntityPersister.insert
(AbstractEntityPersister.java:2656)
at org.hibernate.action.EntityInsertAction.execute
(EntityInsertAction.java:52)
at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:248)
at org.hibernate.engine.ActionQueue.executeActions
(ActionQueue.java:232)
at org.hibernate.engine.ActionQueue.executeActions
(ActionQueue.java:139)
at
org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(
AbstractFlushingEventListener.java:298)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush
(DefaultFlushEventListener.java:27)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1000)
at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:
338)
at org.hibernate.transaction.JDBCTransaction.commit
(JDBCTransaction.java:106)
Caused by: org.firebirdsql.jdbc.FBSQLException: GDS Exception.
335544665. violation of PRIMARY or UNIQUE KEY constraint "INTEG_64"
on table "P_PARTNER"
at org.firebirdsql.jdbc.AbstractPreparedStatement.internalExecute
(AbstractPreparedStatement.java:712)
at org.firebirdsql.jdbc.AbstractPreparedStatement.executeUpdate
(AbstractPreparedStatement.java:172)
at
org.apache.tomcat.dbcp.dbcp.DelegatingPreparedStatement.executeUpdate
(DelegatingPreparedStatement.java:101)
at org.hibernate.jdbc.NonBatchingBatcher.addToBatch
(NonBatchingBatcher.java:23)
at org.hibernate.persister.entity.AbstractEntityPersister.insert
(AbstractEntityPersister.java:2243)
... 30 more
Thanks a lot,
Christian
I am using hibernate to insert data within a firebird 2.0 classic
server. One of my tables has a unique key constraint defined on a
varchar field.
How can I catch this constraint using hibernate and extract a
meaningfull error message? It seems that the hibernates
ConstraintViolationException is not thrown - or used by hibernate in
conjunction with firebird (it gets thrown when using mysql).
I can catch the SQLErrorCode - but it binds me to firebird - it would
be nicer if there is a generic way to handle such behaviour using
build in hibernate exceptions.
The SQLErrorCode is 335544665 but the SQLState is mostly HY000. It
seems, that hibernate uses the first two characters of the sqlstate
to determine the error cathegory (defined in SQLStateConverter.java)
- and thus throws a more specific exception than the
GenericJDBCException exception.
Relevant parts of the stacktrace look like:
org.hibernate.exception.GenericJDBCException: could not insert:
[my.project.model.Partner]
at
org.hibernate.exception.SQLStateConverter.handledNonSpecificException
(SQLStateConverter.java:103)
at org.hibernate.exception.SQLStateConverter.convert
(SQLStateConverter.java:91)
at org.hibernate.exception.JDBCExceptionHelper.convert
(JDBCExceptionHelper.java:43)
at org.hibernate.persister.entity.AbstractEntityPersister.insert
(AbstractEntityPersister.java:2263)
at org.hibernate.persister.entity.AbstractEntityPersister.insert
(AbstractEntityPersister.java:2656)
at org.hibernate.action.EntityInsertAction.execute
(EntityInsertAction.java:52)
at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:248)
at org.hibernate.engine.ActionQueue.executeActions
(ActionQueue.java:232)
at org.hibernate.engine.ActionQueue.executeActions
(ActionQueue.java:139)
at
org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(
AbstractFlushingEventListener.java:298)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush
(DefaultFlushEventListener.java:27)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1000)
at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:
338)
at org.hibernate.transaction.JDBCTransaction.commit
(JDBCTransaction.java:106)
Caused by: org.firebirdsql.jdbc.FBSQLException: GDS Exception.
335544665. violation of PRIMARY or UNIQUE KEY constraint "INTEG_64"
on table "P_PARTNER"
at org.firebirdsql.jdbc.AbstractPreparedStatement.internalExecute
(AbstractPreparedStatement.java:712)
at org.firebirdsql.jdbc.AbstractPreparedStatement.executeUpdate
(AbstractPreparedStatement.java:172)
at
org.apache.tomcat.dbcp.dbcp.DelegatingPreparedStatement.executeUpdate
(DelegatingPreparedStatement.java:101)
at org.hibernate.jdbc.NonBatchingBatcher.addToBatch
(NonBatchingBatcher.java:23)
at org.hibernate.persister.entity.AbstractEntityPersister.insert
(AbstractEntityPersister.java:2243)
... 30 more
Thanks a lot,
Christian