my dog learned polymorphism*
The moose likes Other Application Frameworks and the fly likes translation of Hibernate sql exception to Spring native sql exception Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Frameworks » Other Application Frameworks
Bookmark "translation of Hibernate sql exception to Spring native sql exception" Watch "translation of Hibernate sql exception to Spring native sql exception" New topic
Author

translation of Hibernate sql exception to Spring native sql exception

atul kumar
Greenhorn

Joined: Nov 14, 2008
Posts: 7
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

Joined: Feb 05, 2001
Posts: 17249
    
    6

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

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

Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
atul kumar
Greenhorn

Joined: Nov 14, 2008
Posts: 7
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

Joined: Nov 14, 2008
Posts: 7
translation issue is resolved.
Thanks
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17249
    
    6

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
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: translation of Hibernate sql exception to Spring native sql exception
 
Similar Threads
Simple AOP not working
Controlling Hibernate Session with Spring
Spring 2.5 - Rolling back first method, when the second method throws Runtime ex
What's wrong with this code????
Can you please explain this output - Kind of recursion