嗨!在这里我试图在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不知道我哪里出错了可以有人帮我如何实现这个?
答案 0 :(得分: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);
}