关于HibernateDaoSupport使用的问题,高手指点

06-07-10 wilson751
实现的是一个用户输入用户名、密码,到数据库中验证有无该用户的功能。程序报错:

java.lang.IllegalArgumentException: No SessionFactory specified:

org.snailheart.dao.BaseDAO.openSession(这句出错)

简单说一下程序:

1.创建了一个BaseDAO.java,继承了HibernateDaoSupport,代码如下:

package test.dao;

import org.springframework.orm.hibernate.support.HibernateDaoSupport;

import org.springframework.orm.hibernate.SessionFactoryUtils;

import net.sf.hibernate.Session;

import net.sf.hibernate.Query;

import net.sf.hibernate.HibernateException;

public class BaseDAO extends HibernateDaoSupport {

public Session openSession() {

return SessionFactoryUtils.getSession(getSessionFactory(), false);

}

public Query getQuery(String query) throws HibernateException{

Session session = this.openSession();

return session.createQuery(query);

}

}

2.创建UserDAOHBImpl.java,继承BaseDAO。在其中实现了访问DB的具体

操作,代码如下,为了便于查看,只保留了一个方法:

package test.dao.hibernate;

import java.util.List;

import org.apache.commons.logging.Log;

import org.apache.commons.logging.LogFactory;

import org.springframework.orm.hibernate.support.HibernateDaoSupport;

import net.sf.hibernate.Session;

import net.sf.hibernate.Query;

import net.sf.hibernate.HibernateException;

import test.dao.BaseDAO;

import test.dao.UserDAO;

import test.domain.User;

import test.dto.UserDTO;

public class UserDAOHBImpl extends BaseDAO implements UserDAO {

public User getUser(UserDTO userDTO) {

String userName = userDTO.getUserName();

String pwd = userDTO.getPassword();

String queryStr = "select user from User where User.userName

=:name and User.passwd =:pwd";

Query query = this.getQuery(queryStr);

query.setString("name", userName);

query.setString("pwd", pwd);

List userList = query.list();

if(userList != null) {

user = (User)userList.get(0);

}else {

user = null;

}

}

}

3.在applicationContext.xml中,做相应地配置:

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"

"http://www.springframework.org/dtd/spring-beans.dtd">

<beans>

<bean id="oracleDS"

class="org.springframework.jdbc.datasource.DriverManagerDataSource">

<property name="driverClassName">

<value>oracle.jdbc.driver.OracleDriver</value>

</property>

<property name="url">

<value>jdbc:oracle:thin:@localhost:1521:oradb</value>

</property>

<property name="username">

<value>test</value>

</property>

<property name="password">

<value>test</value>

</property>

</bean>

<bean id="sessionFactory"

class="org.springframework.orm.hibernate.LocalSessionFactoryBean">

<property name="dataSource">

<ref local="oracleDS"/>

</property>

<property name="mappingDirectoryLocations">

<list>

<value>classpath:/org/snailheart/domain/hbm</value>

</list>

</property>

<property name="hibernateProperties">

<props>

<prop key="hibernate.dialect">

net.sf.hibernate.dialect.OracleDialect</prop>

<prop key="hibernate.show_sql">true</prop>

</props>

</property>

</bean>

<bean id="userDAO"

class="org.snailheart.dao.hibernate30.UserDAOHBImpl">

<property name="sessionFactory">

<ref local="sessionFactory" />

</property>

</bean>

</beans>

4.在struts的action类中,调用UserDAOHBImpl的方法

getUser(UserDTO userDTO),查询是否存在当前用户,程序报错:

java.lang.IllegalArgumentException: No SessionFactory specified:

org.snailheart.dao.BaseDAO.openSession

wilson751
2006-07-10 00:24
applicationContext.xml显示有问题,请看这里:

http://www.matrix.org.cn/thread.shtml?topicId=48247&forumId=23

猜你喜欢