• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Exception handling using hibernate template in Spring framework

 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,

I am using Spring with Hibernate in my project.
.There are many methods written in DAO implementation java file and every method is using the same try/catch/finally lines of code which seem redundant to me.
I am told to optimize/refactor the code since the file LOC exceeds 10k. I read somewhere that using HibernateDaoSupport we need not to worry about exceptions or closing the session. It will be taken care of by Spring itself. Could somebody please help me how to proceed or do the needful? I am very much new to Spring
I am pasting below code of one method in DAO layer

 
Ranch Hand
Posts: 196
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are already using spring in your project. You can use Spring AOP for exception handling in your code.
 
Anand Jain
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Piyush Mangal wrote:You are already using spring in your project. You can use Spring AOP for exception handling in your code.



Thanks Piyush. Could you please share some sample code or link for using AOP with exception handling.
But is it necessary to catch the exceptions in DAO layer if we use hibernate template?
 
Piyush Mangal
Ranch Hand
Posts: 196
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This might help http://www.mkyong.com/spring/spring-aop-example-pointcut-advisor/.

No it is not necessary to catch exception in Dao, Spring throws all exceptions as runtime exception so you can take a call where to catch an exception.
 
Bartender
Posts: 1682
7
Android Mac OS X IntelliJ IDE Spring Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I personally like to stick with JPA and only resort to vendor (like hibernate) specific stuff when I have to. With that said being forced to extend classes from the framework or the use of templates is a thing of the past. Rather than write and example myself I found a good one that will get you started. I glanced at it and I think he diid a good job covering the hows and whys. Let me know if anything is not clear.

http://www.baeldung.com/2011/12/02/the-persistence-layer-with-spring-3-1-and-hibernate/

reply
    Bookmark Topic Watch Topic
  • New Topic