-->
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.  [ 4 posts ] 
Author Message
 Post subject: Spring throwing UncategorizedDataAccessException ..
PostPosted: Thu Mar 04, 2004 6:48 am 
Beginner
Beginner

Joined: Tue Nov 18, 2003 10:16 am
Posts: 33
Location: Cluj-Napoca, Romania
I am using hibernate with Spring. My problem is that the exceptions thrown by the JDBC driver are not correct interpreted by Spring (at least not as I see it) .

For example: if i got a FK constraint from the database, Spring throws
org.springframework.orm.hibernate.HibernateJdbcException which is a subclass of UncategorizedDataAccessException .
I was expected a DataIntegrityViolationException instead.

Do I missunderstand the meaning of Spring interpreting the exception?
Or maybe a configuration problem?


I must mention that I got the same problem using Mysql InnoDB and MSSQL.

This is an excerpt of the output the running method generates:

INFO: Creating new JDBC connection to [jdbc:JSQLConnect://192.168.1.21/database=webmin]
Mar 4, 2004 12:35:23 PM net.sf.hibernate.util.JDBCExceptionReporter logExceptions
WARNING: SQL Error: 547, SQLState: 23000
Mar 4, 2004 12:35:23 PM net.sf.hibernate.util.JDBCExceptionReporter logExceptions
SEVERE: INSERT statement conflicted with COLUMN FOREIGN KEY constraint 'FK5AE5775538A56BFB'. The conflict occurred in database 'webmin', table 'menu', column 'id'.
Mar 4, 2004 12:35:23 PM net.sf.hibernate.JDBCException <init>
SEVERE: Could not insert
com.jnetdirect.jsql.u: INSERT statement conflicted with COLUMN FOREIGN KEY constraint 'FK5AE5775538A56BFB'. The conflict occurred in database 'webmin', table 'menu', column 'id'.
at com.jnetdirect.jsql.aq.a(Unknown Source)
at com.jnetdirect.jsql.ae.f(Unknown Source)
at com.jnetdirect.jsql.ae.int(Unknown Source)
at com.jnetdirect.jsql.ae.new(Unknown Source)
at com.jnetdirect.jsql.ak.executeUpdate(Unknown Source)
at net.sf.hibernate.persister.EntityPersister.insert(EntityPersister.java:504)
at net.sf.hibernate.persister.EntityPersister.insert(EntityPersister.java:444)
at net.sf.hibernate.impl.SessionImpl.doSave(SessionImpl.java:717)
at net.sf.hibernate.impl.SessionImpl.save(SessionImpl.java:605)
at net.sf.hibernate.impl.SessionImpl.saveOrUpdate(SessionImpl.java:1202)
at org.springframework.orm.hibernate.HibernateTemplate$2.doInHibernate(HibernateTemplate.java:220)
at org.springframework.orm.hibernate.HibernateTemplate.execute(HibernateTemplate.java:144)
at org.springframework.orm.hibernate.HibernateTemplate.saveOrUpdate(HibernateTemplate.java:218)
at ro.gebs.webmin.dao.hibernate.MenuOptionDAOImpl.saveMenuOption(MenuOptionDAOImpl.java:134)
at ro.gebs.webmin.service.hibernate.AdminServiceImpl.saveMenuOption(AdminServiceImpl.java:189)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:324)
at org.springframework.aop.interceptor.AbstractReflectionInvokerInterceptor.invoke(AbstractReflectionInvokerInterceptor.java:54)
at org.springframework.aop.framework.MethodInvocationImpl.proceed(MethodInvocationImpl.java:216)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:152)
at org.springframework.aop.framework.MethodInvocationImpl.proceed(MethodInvocationImpl.java:216)
at org.springframework.aop.framework.AopProxy.invoke(AopProxy.java:102)
at $Proxy0.saveMenuOption(Unknown Source)
at ro.gebs.webmin.unittests.ExceptionTest.testForeignKeyException(ExceptionTest.java:148)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:324)
at junit.framework.TestCase.runTest(TestCase.java:154)
at junit.framework.TestCase.runBare(TestCase.java:127)
at junit.framework.TestResult$1.protect(TestResult.java:106)
at junit.framework.TestResult.runProtected(TestResult.java:124)
at junit.framework.TestResult.run(TestResult.java:109)
at junit.framework.TestCase.run(TestCase.java:118)
at junit.framework.TestSuite.runTest(TestSuite.java:208)
at junit.framework.TestSuite.run(TestSuite.java:203)
at junit.textui.TestRunner.doRun(TestRunner.java:116)
at junit.textui.TestRunner.start(TestRunner.java:172)
at com.intellij.rt.execution.junit.TextTestRunner.main(TextTestRunner.java:12)
Mar 4, 2004 12:35:23 PM org.springframework.transaction.interceptor.TransactionInterceptor onThrowable
SEVERE: Rolling back transaction on method 'saveMenuOption' due to throwable: JDBC exception on Hibernate data access: Could not insert: INSERT statement conflicted with COLUMN FOREIGN KEY constraint 'FK5AE5775538A56BFB'. The conflict occurred in database 'webmin', table 'menu', column 'id'.; nested exception is:
com.jnetdirect.jsql.u: INSERT statement conflicted with COLUMN FOREIGN KEY constraint 'FK5AE5775538A56BFB'. The conflict occurred in database 'webmin', table 'menu', column 'id'.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 04, 2004 11:33 am 
Beginner
Beginner

Joined: Tue Nov 18, 2003 10:16 am
Posts: 33
Location: Cluj-Napoca, Romania
I have used a debugger on the source code of Spring and I saw that
org.springframework.orm.hibernate.SessionFactoryUtils
will never throw an DataIntegrityViolationException.
I have already noticed that an configurable Interceptor for sql states (or error codes) exists in org.springframework.jdbc.core.SQLStateSQLExceptionTranslator.

The error codes are customisable throught an xml file: sql-error-codes.xml
So, my now question is:

Is there any configuration on which can I set the error codes, like for JdbcTemplate, using hibernate ?

regards, Florin Marcus.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 09, 2004 8:23 am 
Senior
Senior

Joined: Wed Aug 27, 2003 6:04 am
Posts: 161
Location: Linz, Austria
Indeed, you need to set a more sophisticated SQLExceptionTranslator here. HibernateTemplate supports a "jdbcExceptionTranslator" property; HibernateTransactionManager does too.

Pass an appropriate instance of SQLErrorCodeSQLExceptionTranslator in, specifying a DataSource for detection of the database product:

<bean id="myJdbcExceptionTranslator" class="org.springframework.jdbc.support.SQLErrorCodeSQLExceptionTranslator">
<property name="dataSource"><ref bean="myDataSource"/></property>
</bean>

<bean id="myHibernateTemplate" class="org.springframework.orm.hibernate.HibernateTemplate">
<property name="sessionFactory"><ref bean="mySessionFactory"/></property>
<property name="jdbcExceptionTranslator"><ref bean="myJdbcExceptionTranslator"/></property>
</bean>

<bean id="myDao" class="...">
<property name="hibernateTemplate"><ref bean="myHibernateTemplate"/></property>
</bean>

If you want to customize the SQL error codes, provide your own version of Spring's sql-error-codes.xml in the root of the classpath. The default can be found in the org.springframework.jdbc.support package of spring.jar.

Juergen


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 09, 2004 9:10 am 
Beginner
Beginner

Joined: Tue Nov 18, 2003 10:16 am
Posts: 33
Location: Cluj-Napoca, Romania
Thanks!
It really works.

I don't understand why there is no documentation provided byu Spring on this fundamental issue.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 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.