0

你好!在这里,我尝试在 dao 层中使用 hibernateTemplate,之前我使用 sessionfactory 进行检索,它工作正常,但现在我需要实现 hibernateTemplate,当我使用 hibernateTemplate 时它返回 null。这是我的 dispatcher-servlet.xml 和 mydao 代码。

调度程序-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>

我的 dao 文件是

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);
    }

}

在上述 listApprovedrequests 方法的 dao 文件中,我尝试过 hibernateTemplate 但它返回 null。我也使用sessionFactory.getCurrentSession().createCriteria(ApprovedRequests.class).list()(评论过)它的工作正常,但无法使用 hibernatetemplate 不知道我哪里出错了,任何人都可以帮我实现这一目标吗?

4

1 回答 1

1

代替

@Autowired
private SessionFactory sessionFactory;

private HibernateTemplate hibernateTemplate;

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

private SessionFactory sessionFactory;

private HibernateTemplate hibernateTemplate;

@Autowired
public void setSessionFactory(SessionFactory sessionFactory)
{
    this.hibernateTemplate = new HibernateTemplate(sessionFactory);
}
于 2013-05-20T18:12:57.077 回答