aspose file tools*
The moose likes Object Relational Mapping and the fly likes Hibernate and JPA entity with spring framework Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Hibernate and JPA entity with spring framework" Watch "Hibernate and JPA entity with spring framework" New topic
Author

Hibernate and JPA entity with spring framework

Ahsan Habib
Greenhorn

Joined: Sep 17, 2008
Posts: 22
Previously i was using spring jdbc template for persistence. Recently I was trying to use hibernate 3.2 while JPA annotations which eliminates the need for hibernate mapping xml files - all these are manged with Spring framework. Now I encounter one strange problem. I have one worker thread which retrieves some requests periodically and service them accordingly. I wrapped my code inside try{}catch(Throwable e){} within an infinite loop but i noticed that worker thread some times disappears (loop breaks) and it does not enter the catch block! Naturally I was confused with this behaviour since i believe that any exception should enter catch block.

After some googling i found UncaughtExceptionHandler. Inside uncaughtException function i simply log the error and its was great NullPointerException. So i was assuming that while hibernate making some query and finding NULL in some column, it catches the exception and not allowing it to bubble up. But i am not getting why the thread crashed !! It is making me really confused. Can you please show me some light.

Thanks in advance.


Ahsan Habib
SCJP5, SCWCD5
Eduardo Bueno
Ranch Hand

Joined: Jun 04, 2009
Posts: 155
How is hibernate accessing your database?
Ahsan Habib
Greenhorn

Joined: Sep 17, 2008
Posts: 22
Here is the entity pojo -

Dao goes below -

Here is the spring config xml -

Finally here is how worker uses dao -

Thats all. Hope it helps to figure out the big picture of the scenario.
Thank you very much for your time.
Eduardo Bueno
Ranch Hand

Joined: Jun 04, 2009
Posts: 155
I don't see any point where you can get a NullPointerException in your code, except if Spring is not injecting your DAO properly. Consider using getDao() instead of the attribute directly for a better approach. The exception could also be caused in method processJobs, if you consider that null is being returned from the database.
Ahsan Habib
Greenhorn

Joined: Sep 17, 2008
Posts: 22
I agree with you that I should examine method processJobs carefully. But what is not clear to me what why processJobs is not throwing NullPointerException which should be caught at the top level? The exception only caught by UncaughtExcptionHandler. What should be preferred coding convention to address such situation then - try/catch or UncaughtExcptionHandler?
Ahsan Habib
Greenhorn

Joined: Sep 17, 2008
Posts: 22
Holy cow! I pointed out the problem. It is in the worker's infinite loop inside catch block (line number 18 of last code fragment)-

I believe even a dumb guy can imagine what happens when e.getCause() is null which I failed to locate so far. The catch bock was throwing some exception and thts why the loop breaks.

Thanks Bueno for your help so far.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Hibernate and JPA entity with spring framework