Shabarinath Volam
Shabarinath Volam

Reputation: 747

HibernateTemplate returning null

Hi! here I am trying to use hibernateTemplate in the dao layer, earlier I have working with sessionfactory for retrieving and it worked fine but now I need to implement hibernateTemplate, When I am using hibernateTemplate it is returning null. Here is my dispatcher-servlet.xml and mydao code.

dispatcher-servlet.xml:

 <?xml version="1.0" encoding="UTF-8"?>
 <beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
                        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
                        http://www.springframework.org/schema/context
                        http://www.springframework.org/schema/context/spring-context-3.0.xsd
                        http://www.springframework.org/schema/tx
                        http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">

<context:property-placeholder location="classpath:jdbc.properties" />
<context:component-scan base-package="net.roseindia" />

<tx:annotation-driven transaction-manager="hibernateTransactionManager"/>

<bean id="jspViewResolver"
    class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="viewClass"
        value="org.springframework.web.servlet.view.JstlView" />
    <property name="prefix" value="/WEB-INF/view/" />
    <property name="suffix" value=".jsp" />
</bean>

<bean id="dataSource"
    class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="${database.driver}" />
    <property name="url" value="${database.url}" />
    <property name="username" value="${database.user}" />
    <property name="password" value="${database.password}" />
</bean>

<bean id="sessionFactory"
    class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="annotatedClasses">
        <list>
            <value>net.roseindia.model.Article</value>
            <value>net.roseindia.model.User</value>
            <value>net.roseindia.model.Register</value>
            <value>net.roseindia.model.UserSignup</value>
            <value>net.roseindia.model.BusRequest</value>
            <value>net.roseindia.model.ApprovedRequests</value>
        </list>
    </property>
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">${hibernate.dialect}</prop>
            <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>             
        </props>
    </property>
 </bean>

   <bean id="hibernateTransactionManager"
    class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory" />
   </bean>
</beans>

and my dao file is

package net.roseindia.dao;

import java.util.List;

import net.roseindia.model.ApprovedRequests;
import net.roseindia.model.BusRequest;

import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.stereotype.Repository;

@Repository("busRequestDao")
public class BusRequestDaoImpl implements BusRequestDao {

    @Autowired
    private SessionFactory sessionFactory;

    private HibernateTemplate hibernateTemplate;

    public void setSessionFactory(SessionFactory sessionFactory)
    {
        this.hibernateTemplate = new HibernateTemplate(sessionFactory);
    }

    @Override
    public void addRequest(BusRequest busrequest) {
        // TODO Auto-generated method stub
        sessionFactory.getCurrentSession().saveOrUpdate(busrequest);

    }

    @SuppressWarnings("unchecked")
    @Override
    public List<BusRequest> listrequests() {
        // TODO Auto-generated method stub
        return (List<BusRequest>) sessionFactory.getCurrentSession().createCriteria(BusRequest.class).list();
    }

    @SuppressWarnings("unchecked")
    @Override
    public List<ApprovedRequests> listApprovedrequests() {
        // TODO Auto-generated method stub
    //  return (List<ApprovedRequests>) sessionFactory.getCurrentSession().createCriteria(ApprovedRequests.class).list();
         return hibernateTemplate.find("from ApprovedRequests");
    }


    @Override
    public void approveRequest(ApprovedRequests approvedRequests) {
        sessionFactory.getCurrentSession().saveOrUpdate(approvedRequests);
    }

}

In the above dao file for listApprovedrequests method i have tried hibernateTemplate but it is returning null. I have also used sessionFactory.getCurrentSession().createCriteria(ApprovedRequests.class).list()(commented one) its working fine but unable to work with hibernatetemplate dont know where i am going wrong can anybody help me how to achieve this??

Upvotes: 0

Views: 3835

Answers (1)

Ori Dar
Ori Dar

Reputation: 19020

Replace

@Autowired
private SessionFactory sessionFactory;

private HibernateTemplate hibernateTemplate;

public void setSessionFactory(SessionFactory sessionFactory)
{
    this.hibernateTemplate = new HibernateTemplate(sessionFactory);
}

with

private SessionFactory sessionFactory;

private HibernateTemplate hibernateTemplate;

@Autowired
public void setSessionFactory(SessionFactory sessionFactory)
{
    this.hibernateTemplate = new HibernateTemplate(sessionFactory);
}

Upvotes: 1

Related Questions