IntelliJ Java IDE
The moose likes Other Application Frameworks and the fly likes NPE at getHibernateTemplate( ) Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Application Frameworks » Other Application Frameworks
Reply Bookmark "NPE at getHibernateTemplate( )" Watch "NPE at getHibernateTemplate( )" New topic
Author

NPE at getHibernateTemplate( )

Hussein Baghdadi
clojure forum advocate
Bartender

Joined: Nov 08, 2003
Posts: 3334

Hi.
My application using Spring and Hibernate as a persistenting technology.
Here is the class that uses Hibernate :

In the application context, I created a datasource, a session factory, and an instance of
StoringEvents successfully (no excpetions throws).

(It worth noting that I didn't create a transaction manager).
But when trying to run the application (web one), I got a NullPointerException at getHibernateTemplate().save(le);
The getHibernateTemplate( ) returns null.
Any ideas ?
Thanks.
Gregg Bolinger
Sheriff

Joined: Jul 11, 2001
Posts: 15040

So how do you suppose in your config your class is going to get injected with a HibernateTemplate object? I am using iBatis, but the concept is exactly the same. Here is my config...


[ January 14, 2006: Message edited by: Gregg Bolinger ]

My Blog | DZone Articles
Hussein Baghdadi
clojure forum advocate
Bartender

Joined: Nov 08, 2003
Posts: 3334

So, creating a transaction manager is mandatory ?
Please, check this :
http://www.coderanch.com/t/60069/oa/JSP-Servlets-Spring
My original problem originated here ...
Gregg Bolinger
Sheriff

Joined: Jul 11, 2001
Posts: 15040

One doesn't have anything to do with the other. You need to inject the HibernateTemplate object into your dao in order to use it.
Ken Krebs
Ranch Hand

Joined: Nov 27, 2002
Posts: 451
Actually, the HibernateDaoSupport Javadoc says:

Convenient super class for Hibernate data access objects.

Requires a SessionFactory to be set, providing a HibernateTemplate based on it to subclasses. Can alternatively be initialized directly via a HibernateTemplate, to reuse the latter's settings like SessionFactory, exception translator, flush mode, etc.

This base class is mainly intended for HibernateTemplate usage but can also be used when working with SessionFactoryUtils directly, e.g. in combination with HibernateInterceptor-managed Sessions. Convenience getSession and releaseSession methods are provided for that usage style.

This class will create its own HibernateTemplate if only a SessionFactory is passed in. The allowCreate flag on that HibernateTemplate will be true by default. A custom HibernateTemplate instance can be used through overriding createHibernateTemplate.


So the configuration you've used should work without having to manually inject a HibernateTemplate. This technique is standard and is demonstrated in the PetClinic sample app. All the DaoSupport classes work this way. Perhaps something else is missing that is causing the NPE. Maybe you should post the full stack trace.

BTW, DriverManagerDataSource is not a pooling DataSource and is meant primarily for testing. PetClinic shows alternative DataSource definitions that provide pooled Connections you can use in a production environment.


kktec<br />SCJP, SCWCD, SCJD<br />"What we observe is not nature itself, but nature exposed to our method of questioning." - Werner Heisenberg
bob daly
Greenhorn

Joined: Apr 09, 2006
Posts: 3
I'm experiencing the NPE on getHibernateTemplate as well - and having a difficult time finding out what my problem is.

I do not see any exceptions related to the sessionFactory
(org.springframework.orm.hibernate3.LocalSessionFactoryBean) on app start-up, and all of the Hibernate mappings seem to go smoothly.

I created a constructor in one of my classes that extends HibernateDAOSupport and call this.getSessionFactory; it's always null.

Any advice on how I should be troubleshooting this?
Chris Mathews
Ranch Hand

Joined: Jul 18, 2001
Posts: 2712
"rf daly"

Thanks for joining JavaRanch, please take some time to review the JavaRanch Name Policy then update your publicly displayed name in your profile so that it complies.

I would also suggest creating your own thread for questions instead of tacking onto an existing thread, if you feel that the problem is related it is probably helpful to include a link back to the original thread as well.
 
 
subject: NPE at getHibernateTemplate( )
 
Threads others viewed
unsatisfied Dependancy exception
Error: Error registering bean with name 'transactionManager'
Spring - Hiber. Transaction is not working...
Spring And Hibernate (LocalSessionFactoryBean)
Hiberante transaction in Spring
IntelliJ Java IDE

cast iron skillet 49er

more from paul wheaton's glorious empire of web junk: cast iron skillet diatomaceous earth rocket mass heater sepp holzer raised garden beds raising chickens lawn care CFL flea control missoula heat permaculture