aspose file tools*
The moose likes Spring and the fly likes Exception handling using hibernate template in Spring framework Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Spring
Bookmark "Exception handling using hibernate template in Spring framework" Watch "Exception handling using hibernate template in Spring framework" New topic
Author

Exception handling using hibernate template in Spring framework

Anand Jain
Greenhorn

Joined: Jul 27, 2009
Posts: 18
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

Joined: Jan 22, 2007
Posts: 196
You are already using spring in your project. You can use Spring AOP for exception handling in your code.
Anand Jain
Greenhorn

Joined: Jul 27, 2009
Posts: 18
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

Joined: Jan 22, 2007
Posts: 196
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

Joined: Mar 07, 2010
Posts: 1649
    
    7

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/


[How To Ask Questions][Read before you PM me]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Exception handling using hibernate template in Spring framework