为啥我单元测试时候既可以开启Session,在结合了struts1以后我的Session就开不起来了

123465Wzf 2015-10-01 09:54:52

数据库中的admin表中的数据
admin 王志飞 admin
这是单元测试的代码
package test;

import java.util.Iterator;
import java.util.List;

import org.hibernate.Session;

import com.fly.model.Admin;
import com.fly.util.HibernateUtils;

import junit.framework.TestCase;

public class test extends TestCase {
public void test1(){
Admin admin = null;
Session session = null;
try {
session = HibernateUtils.getSession();
session.beginTransaction();
/**
* 采用list查询实体对象会发出一条查询语句,取得实体对象数据
*
*/
List<Admin> adminlist = session
.createQuery(
"from Admin where loginname=? and password=?")
.setParameter(0, "admin")
.setParameter(1, "admin")
.list();
for (Iterator<Admin> iter = adminlist.iterator(); iter.hasNext();) {
admin = (Admin) iter.next();
System.out.println(admin.getLoginName());
System.out.println(admin.getName());
}
session.getTransaction().commit();
} catch (Exception e) {
e.printStackTrace();
session.getTransaction().rollback();
} finally {
HibernateUtils.closeSession(session);
}
}
}
单元测试 的结果
Hibernate: select admin0_.loginName as loginName0_, admin0_.name as name0_, admin0_.password as password0_ from admin admin0_ where loginname=? and admin0_.password=?
admin
王志飞
主项目
public Admin AdminLogin(String loginName, String password) {
Admin admin = null;
Session session = null;
try {
session = HibernateUtils.getSession();
就是这里出现了问题Session不能开启就直接执行finally里的关闭Session方法
然后就报Session不能被找到
session.beginTransaction();
@SuppressWarnings("unchecked")
List<Admin> adminlist = session
.createQuery("from Admin where loginname=? and password=?")
.setParameter(0, loginName).setParameter(1, password)
.list();
Iterator<Admin> iter = adminlist.iterator();
if(iter.hasNext()) {
admin = (Admin) iter.next();
/*System.out.println(admin.getLoginName());
System.out.println(admin.getName());*/
}else{
throw new RuntimeException();
}
session.getTransaction().commit();
} catch (Exception e) {
e.printStackTrace();
session.getTransaction().rollback();
} finally {
HibernateUtils.closeSession(session);
}
return admin;
}
这是tomcat报的错误Session没有找到
--------------------------------------------------------------------------------

type Exception report

message Servlet execution threw an exception

description The server encountered an internal error that prevented it from fulfilling this request.

exception

javax.servlet.ServletException: Servlet execution threw an exception
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
com.fly.filter.CharsetEncodingFilter.doFilter(CharsetEncodingFilter.java:28)


root cause

java.lang.NoClassDefFoundError: org/hibernate/Session
com.fly.message.AdminManager.AdminLogin(AdminManager.java:150)
com.fly.action.LoginAction.execute(LoginAction.java:32)
org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:431)
org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:236)
org.apache.struts.action.ActionServlet.process(ActionServlet.java:1196)
org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:432)
javax.servlet.http.HttpServlet.service(HttpServlet.java:646)
javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
com.fly.filter.CharsetEncodingFilter.doFilter(CharsetEncodingFilter.java:28)


root cause

java.lang.ClassNotFoundException: org.hibernate.Session
org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1720)
org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1571)
com.fly.message.AdminManager.AdminLogin(AdminManager.java:150)
com.fly.action.LoginAction.execute(LoginAction.java:32)
org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:431)
org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:236)
org.apache.struts.action.ActionServlet.process(ActionServlet.java:1196)
org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:432)
javax.servlet.http.HttpServlet.service(HttpServlet.java:646)
javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
com.fly.filter.CharsetEncodingFilter.doFilter(CharsetEncodingFilter.java:28)


note The full stack trace of the root cause is available in the Apache Tomcat/7.0.54 logs.


--------------------------------------------------------------------------------

Apache Tomcat/7.0.54

...全文
60 点赞 收藏 3
写回复
3 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
123465Wzf 2015-10-03
引用 1 楼 Defonds的回复:
检查你的 hibernate 包是否在 tomcat 运行的 classpath 里头
谢谢,解决了,非常感谢你的指教
回复
123465Wzf 2015-10-02
引用 1 楼 Defonds的回复:
检查你的 hibernate 包是否在 tomcat 运行的 classpath 里头
这个如何查看啊,听起来感觉就是你聊的这个错误,我试试吧不会了再请教你
回复
Defonds 2015-10-02
检查你的 hibernate 包是否在 tomcat 运行的 classpath 里头
回复
相关推荐
发帖
Web 开发
创建于2007-09-28

8.0w+

社区成员

Java Web 开发
申请成为版主
帖子事件
创建了帖子
2015-10-01 09:54
社区公告
暂无公告