關於HibernateDaoSupport使用的問題,高手指點

wilson751發表於2006-07-10
實現的是一個使用者輸入使用者名稱、密碼,到資料庫中驗證有無該使用者的功能。程式報錯:
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

相關文章