Spring+Hibernate,但是使用Spring管理的DAO时,getHibernateTemplate方法得到的总是null,结果什么都查询不了
Spring、hibernate的配置文件,DAO类和hibernate的hbm映射文件都是用myeclipse自动生成的,但是就是出错。
我说一下我的配置
hibernate的配置:hibernate.cfg.xml
<hibernate-configuration> <session-factory>
<property name="connection.username">ppi</property>
<property name="connection.url">
jdbc:oracle:thin:@192.168.99.150:1521:orcl
</property>
<property name="dialect">
org.hibernate.dialect.Oracle9Dialect
</property>
<property name="myeclipse.connection.profile">Oracle</property>
<property name="connection.password">ppidba</property>
<property name="connection.driver_class">
oracle.jdbc.driver.OracleDriver
</property>
<mapping resource="test/Interactor.hbm.xml" />
<mapping resource="test/Project.hbm.xml" />
                ..........................
Spring的配置:applicationContext.xml,指定了hibernate的配置文件路径和sessionFactory及DAO
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation"
value="classpath:hibernate.cfg.xml">
</property>
</bean>
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    <property name="sessionFactory">
      <ref local="sessionFactory"/>
    </property>
  </bean>
<bean id="InteractorDAO" class="test.InteractorDAO">
<property name="sessionFactory">
<ref bean="sessionFactory"></ref>
</property>
</bean>
<bean id="ProjectDAO" class="test.ProjectDAO">
<property name="sessionFactory">
<ref bean="sessionFactory"></ref>
</property>
</bean>
例如InteractorDAO的代码:
public Interactor findById(java.lang.String id) {
log.debug("getting Interactor instance with id: " + id);
try {
Interactor instance = (Interactor) getHibernateTemplate().get(
"test.Interactor", id);
return instance;
} catch (RuntimeException re) {
log.error("get failed", re);
throw re;
}
}然后写一个Test测试InteractorDAO类
package testDAO;import test.InteractorDAO;
import junit.framework.TestCase;public class DAOTest extends TestCase {
private InteractorDAO in ; protected void setUp() throws Exception {
super.setUp();
} protected void tearDown() throws Exception {
super.tearDown();
}
public void testInteractionDAO(){
assertNotNull(in.findById("001"));
} public void setIn(InteractorDAO in) {
this.in = in;
}}
每次执行到Interactor instance = (Interactor) getHibernateTemplate().get(
"test.Interactor", id);就出错了,getHibernateTemplate()是空的
Test中改成InteractorDAO interactorDAO = new InteractorDAO();也不行,到底是因为什么呢?
刚刚看spring,稀里糊涂的

解决方案 »

  1.   

    没有看到你获得spring上下文的代码
      

  2.   

    如果是在web里用DAO,而不是测试方法里调用,该如何得到DAO类呢?
      

  3.   

    1楼的,不太明白,怎么获得spring上下文?
      

  4.   

    在web里得到Dao方式有很多,这里介绍一种与struts整合调用方式:
    第一步,在struts-config.xml中配置插件
    代码如下:
    struts-config.xml<plug-in
    className="org.springframework.web.struts.ContextLoaderPlugIn">
    <set-property property="contextConfigLocation"
    value="/WEB-INF/applicationContext.xml" />
    </plug-in>第二步,让Action继承自org.springframework.web.struts.ActionSupport 或DispatchActionSupport,public class TestAction extends DispatchActionSupport {
      ...第三步,获得Dao对象
    ...
    public ActionForward Test(ActionMapping mapping, ActionForm form,
    HttpServletRequest request, HttpServletResponse response) {
    ApplicationContext ctx = this.getWebApplicationContext();
    TestDAO dao = (TestDAO) ctx.getBean("testDAO");
    ...就这样就获得了Dao,建议你买一本《spring in action》看一下
      

  5.   

    我用的是jsf
    不知道这么用dao对不对,为什么还是报空指针的错误?public class InteractionList extends BaseMethod{
    private int totalInteraction;
    private String method;

    private InteractionListDAO interactionListDAO;public String listInteractionByMethod(){
    method = getRequest().getParameter("method");
    SummaryAssemble summaryAssemble = new SummaryAssemble();
    totalInteraction = interactionListDAO.getCountOfInteractionByMethod(method);
    List list = interactionListDAO.getInteractionSummaryList("method",method); if (list != null) {
    summarylist = summaryAssemble.getInteractionSummaryList(list);
    return "Browse";
    } else {
    return "noResult";
    }
    }
    public void setInteractionListDAO(InteractionListDAO interactionListDAO) {
    this.interactionListDAO = interactionListDAO;
    }
    public ArrayList<InteractionSummary> getSummarylist() {
    return summarylist;
    } public void setSummarylist(ArrayList<InteractionSummary> summarylist) {
    this.summarylist = summarylist;
    } public int getTotalInteraction() {
    return totalInteraction;
    } public void setTotalInteraction(int totalInteraction) {
    this.totalInteraction = totalInteraction;
    }