Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

HibernateTemplate not working

 
Ravi Kiran Va
Ranch Hand
Posts: 2234
Eclipse IDE Firefox Browser Redhat
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please see the code and guide me if i am doing anything wrong here :



Inside MYDAO class which is implemented in Hibernate :




 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ravi Kiran Va
Ranch Hand
Posts: 2234
Eclipse IDE Firefox Browser Redhat
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1374
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As the MyDAOClass does not have a property sessionFactory ??

Inheritance. Did you check the HibernateDaoSupport class?
 
Ravi Kiran Va
Ranch Hand
Posts: 2234
Eclipse IDE Firefox Browser Redhat
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vishal , I am not using suppourt of HIbernateDAOSuppourt base class .


This was simply a HibernateTemplate .
 
Vikas Kapoor
Ranch Hand
Posts: 1374
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2234
Eclipse IDE Firefox Browser Redhat
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1374
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic