Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

translation of Hibernate sql exception to Spring native sql exception

 
atul kumar
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
for testing...i am throwing ConstraintViolationException in getexception() method of DOAimplementaion class and catching it at testcase method
test_getException()

code snippet is given below...
But it is not getting translated...please advise...

DAOimplementation--

@Repository
public class AssetDaoImpl extends GenericDaoImpl<Asset, Long> implements AssetDAO {

....

public void getException() throws ConstraintViolationException{

System.out.println("exception in assetDao");
throw new ConstraintViolationException("sqlgrammerexception in daoimp",new SQLException("3"),"abc");
// throw new HibernateException("abc");
//throw new SQLGrammarException("sqlgrammerexception in daoimp",new SQLException("3"),"abc");
}

.......


Testcasefile

public class AssetDaoTest{
....


@Test
public void test_getException() {
try{
System.out.println("1-----1");
assetDao.getException();
System.out.println("2----2");
}

//catch(HibernateException e){
//System.out.println("'catch--HibernateException'"+e);
//}

catch(DataAccessException e){ //not able to catch the exception--
System.out.println("catch--DataAccessException"+e);
System.out.println("3----3");
}


....
}


spring xml file contains--

have tried using-- PersistenceExceptionTranslationPostProcessor

<context:component-scan base-package="ca.aeso.dt.dao.impl" />
<tx:annotation-driven/>

<bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" />

or

we can translate using PersistenceExceptionTranslationInterceptor...given below..

<bean id="persistenceExceptionInterceptor" class="org.springframework.dao.support.PersistenceExceptionTranslationInterceptor" />
<aop:config proxy-target-class="true">
<aop:advisor pointcut="execution(* @org.springframework.stereotype.Repository *.*(..))" advice-ref="persistenceExceptionInterceptor" />
</aop:config>
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
<bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" />

That is all you should need to get exception translation from HibernateException into DataAccessException hierarchy.

Mark
 
atul kumar
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
it is throwing error given below

Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor#0' defined in class path resource [bean.xml]: Initialization of bean failed; nested exception is java.lang.IllegalStateException: No persistence exception translators found in bean factory. Cannot perform exception translation.
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:480)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory$1.run(AbstractAutowireCapableBeanFactory.java:409)
at java.security.AccessController.doPrivileged(Native Method)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:380)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:264)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:261)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:185)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:164)
at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:880)
at org.springframework.context.support.AbstractApplicationContext.registerBeanPostProcessors(AbstractApplicationContext.java:596)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:365)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
at com.apress.springrecipes.course.Main.main(Main.java:14)
Caused by: java.lang.IllegalStateException: No persistence exception translators found in bean factory. Cannot perform exception translation.
at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.detectPersistenceExceptionTranslators(PersistenceExceptionTranslationInterceptor.java:125)
at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.<init>(PersistenceExceptionTranslationInterceptor.java:78)
at org.springframework.dao.annotation.PersistenceExceptionTranslationAdvisor.<init>(PersistenceExceptionTranslationAdvisor.java:70)
at org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor.setBeanFactory(PersistenceExceptionTranslationPostProcessor.java:97)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1326)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:473)
... 14 more
 
atul kumar
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
translation issue is resolved.
Thanks
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by atul kumar:
translation issue is resolved.
Thanks


Can you post what the probelm was and what you did to resolve it, for others that later read this thread.

Thanks

Mark
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic