• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Junilu Lacar
  • Martin Vashko
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Scott Selikoff
  • salvin francis
  • Piet Souris

NPE at getHibernateTemplate( )

 
clojure forum advocate
Posts: 3479
Mac Objective C Clojure
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 15304
6
Mac OS X IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Hussein Baghdadi
clojure forum advocate
Posts: 3479
Mac Objective C Clojure
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So, creating a transaction manager is mandatory ?
Please, check this :
https://coderanch.com/t/60069/oa/JSP-Servlets-Spring
My original problem originated here ...
 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Mac OS X IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 451
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Ranch Hand
Posts: 2713
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"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.
 
Well behaved women rarely make history - Eleanor Roosevelt. tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!