发帖    主题    评论    推荐    标签    作者    订阅    查搜    注册   登陆   关注
 
面向对象 设计模式 领域驱动设计 企业架构 框架 开发教程 微服务 大数据 扩展性 并发编程 事件驱动 分布式 SOA

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

2006-07-10 00:14
赞助商链接

实现的是一个用户输入用户名、密码,到数据库中验证有无该用户的功能。程序报错:
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

2006-07-10 00:24

applicationContext.xml显示有问题,请看这里:
http://www.matrix.org.cn/thread.shtml?topicId=48247&forumId=23

赞助商链接

赞助商链接

返回顶部

移动版 关于本站 使用帮助 联系管理员 最佳分辨率1366x768
OpenSource JIVEJDON Powered by JdonFramework Code © 2002-20 jdon.com