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

Exception handling using hibernate template in Spring framework

 
Anand Jain
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • 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

 
Piyush Mangal
Ranch Hand
Posts: 196
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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.
 
Bill Gorder
Bartender
Posts: 1682
7
Android IntelliJ IDE Linux Mac OS X Spring
  • Mark post as helpful
  • send pies
  • 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/

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic