-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: Catch Exception for duplicate key on unique index
PostPosted: Fri Oct 15, 2004 8:05 am 
Newbie

Joined: Fri Oct 15, 2004 7:47 am
Posts: 1
Hi,
I'd like to catch this kind of Exception (duplicate key on unique index) because in this case my Application must have a special behavior. I don't know how I can recognize this error and catch it. Another problem is that my application will be installed with different databases (MS SQL Server or Oracle or DB2 ect.) and I'd like to write "generic" code....
Anyone can help me?
I'm sorry for my bad bad English!!!!
Bye,
Sara

Hibernate version:
2.1
Mapping documents:

Code between sessionFactory.openSession() and session.close():
tx = session.beginTransaction();
log.debug("SAVE_OR_UPDATE --> "+po);
session.saveOrUpdate(po);
commitTransaction(tx);

Full stack trace of any exception that occurs:
java.sql.SQLException: [Microsoft][SQLServer 2000 Driver for JDBC][SQLServer]Cannot insert duplicate key row in object 'FDIPEND' with unique index 'uniq_cdutente'.
at com.microsoft.jdbc.base.BaseExceptions.createException(Unknown Source)
at com.microsoft.jdbc.base.BaseExceptions.getException(Unknown Source)
at com.microsoft.jdbc.sqlserver.tds.TDSRequest.processErrorToken(Unknown Source)
at com.microsoft.jdbc.sqlserver.tds.TDSRequest.processReplyToken(Unknown Source)
at com.microsoft.jdbc.sqlserver.tds.TDSRPCRequest.processReplyToken(Unknown Source)
at com.microsoft.jdbc.sqlserver.tds.TDSRequest.processReply(Unknown Source)
at com.microsoft.jdbc.sqlserver.SQLServerImplStatement.getNextResultType(Unknown Source)
at com.microsoft.jdbc.base.BaseStatement.commonTransitionToState(Unknown Source)
at com.microsoft.jdbc.base.BaseStatement.postImplExecute(Unknown Source)
at com.microsoft.jdbc.base.BasePreparedStatement.postImplExecute(Unknown Source)
at com.microsoft.jdbc.base.BaseStatement.commonExecute(Unknown Source)
at com.microsoft.jdbc.base.BaseStatement.executeUpdateInternal(Unknown Source)
at com.microsoft.jdbc.base.BasePreparedStatement.executeUpdate(Unknown Source)
at com.mchange.v2.c3p0.impl.C3P0PreparedStatement.executeUpdate(C3P0PreparedStatement.java:68)
at net.sf.hibernate.impl.NonBatchingBatcher.addToBatch(NonBatchingBatcher.java:22)
at net.sf.hibernate.persister.EntityPersister.update(EntityPersister.java:683)
at net.sf.hibernate.persister.EntityPersister.update(EntityPersister.java:633)
at net.sf.hibernate.impl.ScheduledUpdate.execute(ScheduledUpdate.java:52)
at net.sf.hibernate.impl.SessionImpl.executeAll(SessionImpl.java:2308)
at net.sf.hibernate.impl.SessionImpl.execute(SessionImpl.java:2262)
at net.sf.hibernate.impl.SessionImpl.flush(SessionImpl.java:2187)
at net.sf.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:61)
at com.sinapsi.share.persistance.hibernate.HibernateSessionManager.commitTransaction(HibernateSessionManager.java:133)
at com.dm.dao.AbstractDAO.commitTransaction(AbstractDAO.java:230)
at com.dm.dao.AbstractDAO.saveOrUpdate(AbstractDAO.java:161)

Name and version of the database you are using:
MS SQL SERVER 2000
The generated SQL (show_sql=true):
STATEMENT --> update FDIPEND set NMCOGNOME= 'FRATTINI' , IDAZIENDA= 4 , IDCODSEL1= '' , NMNOME= 'FRANCESCA' , IDUTENTEVALID= '' , IDUTENTEMOD= '' , CDFISCAL= 'FRTFNC96R52E730L' , IDCODSEL2= '' , IDSDIC= '0' , CDDIPENDENTE= '5' , IDCODSEL3= '' , IDRICHIESTA= 2 , TXPASSWORD= '554113e3cbedbc9aff' , CDPAGA= '' , TXMAIL= 'SFRATTINI@DATAMANAGEMENT.IT' , FLBENVENUTO= '0' , CDUTENTE= 'LLRBBR75A62H199I' , DTCREAPWD= '' , DTLASTLOGIN= '' , TXULTPWD1= '' , TXULTPWD2= '' , TXULTPWD3= '' , TXULTPWD4= '' , FLREVOCA= '' where ID= ''
Debug level Hibernate log excerpt:


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 15, 2004 8:28 am 
Expert
Expert

Joined: Thu Jan 29, 2004 2:31 am
Posts: 362
Location: Switzerland, Bern
There's no other way than catching an SQLException or a HibernateException and parse the string. Unfortunately this string is different for every DB and can even be localized!

Be sure to stick to what I've written at the end of topic http://forum.hibernate.org/viewtopic.php?t=935195. You cannot build your business logic on top of catching these execptions within one session.

HTH
Ernst


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.