wood burning stoves 2.0*
The moose likes Other Application Frameworks and the fly likes HibernateTemplate not working Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Frameworks » Other Application Frameworks
Bookmark "HibernateTemplate not working " Watch "HibernateTemplate not working " New topic
Author

HibernateTemplate not working

Ravi Kiran Va
Ranch Hand

Joined: Apr 18, 2009
Posts: 2234

Please see the code and guide me if i am doing anything wrong here :



Inside MYDAO class which is implemented in Hibernate :





Save India From Corruption - Anna Hazare.
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17249
    
    6

OK, HibernateTemplate should be what you are injecting into you DAO, to make it the simplest.

So you define HibernateTemplate in xml passing it a SessionFactory.

In your DAO code it should be

private HibernateTemplate hibernateTemplate;

public void setHibernateTemplate(HibernateTemplate hibernateTemplate) { .... }


getter and setter method with the same naming as the property. Getters and Setters are actually defined by the JavaBean JSR. You should not have different names like

private String someVar;

public void setVar(String someVar)

it should be

public void setSomeVar(String someVar)

Because the property is called someVar, the setter and getter needs to always be called setSomeVar and getSomeVar. Not some other variation.

Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Ravi Kiran Va
Ranch Hand

Joined: Apr 18, 2009
Posts: 2234

Thanks Mark .That was nice of you .


I am having a question here , how this works internally ? As the MyDAOClass does not have a property sessionFactory ??



Vikas Kapoor
Ranch Hand

Joined: Aug 16, 2007
Posts: 1374
As the MyDAOClass does not have a property sessionFactory ??

Inheritance. Did you check the HibernateDaoSupport class?
Ravi Kiran Va
Ranch Hand

Joined: Apr 18, 2009
Posts: 2234

Vishal , I am not using suppourt of HIbernateDAOSuppourt base class .


This was simply a HibernateTemplate .
Vikas Kapoor
Ranch Hand

Joined: Aug 16, 2007
Posts: 1374
Ok so you don't want to use HibernateDaoSupport.

You need to define your bean definition.HibernateTemplate would be the property to that bean definition with proper name and getter-setter. That will take you to Hibernatetempalate bean definition , where you give SessionFactory as property. You don't need to have SessionFactory property to your DAO.

But why are you doing this? The use of HibernateTemplate is discouraged over Session interface. All you need to do is to extend the HibernaeDaoSupport class and provide set sessionfactory bean. And get the session through getSession method. and perform basic CRUD operations.

Ravi Kiran Va
Ranch Hand

Joined: Apr 18, 2009
Posts: 2234

I had found 3 approaches for Spring and Hibernate integration by using HibernateTemplate or a HibernateDaoSupport or by DAO with the Dependency Injection pattern.

I could not grab the best approach among these . COuld you please tell me what approach to follow .


Let me answer my question :

Hibernate API

Advantage is we are comfortable with Hibernate and directly use Hibernate .

The disadvantage of use the Hibernate API is that you don't get the benefits of Spring's DataAccessException translation. Which, depending on your shop, you might care about this.

Generally speaking, if you're committed to using Hibernate, this is not a bad choice.

HibernateTemplate
This is the first level of Spring's coding support for Hibernate. It eliminates the need for doing anything with the Hibernate Session, at all. It looks for a current Hibernate Session for you; if one exists, it executes the requested operation on that session. This is great for turning your simpler Hibernate code into one-liners.

Also, Hibernate Exceptions are automatically converted to Spring's DataAccessException hierarchy.

HibernateDaoSupport
This is the second level of boilerplate removal Spring addresses with respect to Hibernate. This is a base class from which your DAOs extend. HibernateDaoSupport wraps a HibernateTemplate and includes the corresponding getter/setter definitions you would normally have to duplicate across your DAOs.

Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17249
    
    6

Vishal Pandya wrote:
As the MyDAOClass does not have a property sessionFactory ??

Inheritance. Did you check the HibernateDaoSupport class?


No, please do not do this. Do not extend a Spring specific class here.

You should not inject the sessionFactory to your DAO, you need to inject the HibernateTemplate into your dao. get rid of your setter/getter for sessionFactory. Make a setter/getter for hibernateTemplate. And in your xml the property should be

<property name="hibernateTemplate" ref="hibernateTempalte"/>

Mark
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17249
    
    6

Vishal Pandya wrote:Ok so you don't want to use HibernateDaoSupport.

You need to define your bean definition.HibernateTemplate would be the property to that bean definition with proper name and getter-setter. That will take you to Hibernatetempalate bean definition , where you give SessionFactory as property. You don't need to have SessionFactory property to your DAO.

But why are you doing this? The use of HibernateTemplate is discouraged over Session interface. All you need to do is to extend the HibernaeDaoSupport class and provide set sessionfactory bean. And get the session through getSession method. and perform basic CRUD operations.



true. Personally, I have my DAO/Repositories have a private SessionFactory sessionFactory property with a setter for it, and use <property name="sessionFactory" ref="sessionFactory"

Sorry, the code i was looking at here was confusing me. ;)

Mark>
Vikas Kapoor
Ranch Hand

Joined: Aug 16, 2007
Posts: 1374
Paradox? In your second last post you said don't do it and in your last post you said true.



PersistenceFacadeImpl extends HibernateDaoSupport and hence sessionFactory is available through inheritance. What's wrong with this approach?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: HibernateTemplate not working
 
Similar Threads
Unable to connect to database
Spring DAO + Hibernate; is this right?
why to extend HibernateDaoSupport
Spring and Hibernate Transaction Management
Using HibernateTemplate inside EJB