关于单用Hibernate 单用时出现的问题,取不到session
java.lang.NoSuchMethodError: org.hibernate.SessionFactory.getCurrentSession()Lorg/hibernate/classic/Session;
我用Junit测试 可以通过
@Test
public void testExist() throws Exception {
UserService us = new UserServiceImpl();
User u = new User();
u.setUsername("11");
boolean exist = us.exist(u);
Assert.assertEquals(true, exist);
}
但是在页面上 我输入 提交到registerDeal.jsp页面 就会报错
java.lang.
NoSuchMethodError: org.hibernate.SessionFactory.getCurrentSession()Lorg/hibernate/classic/Session;
String username = request.getParameter("username");
String password = request.getParameter("password");
User u = new User();
u.setUsername(username);
u.setPassword(password);
UserService userSservice = new UserServiceImpl();
boolean exist = userSservice.exist(u);
if (exist) {
response.sendRedirect("registerFail.jsp");
return;
}
userSservice.add(u);
response.sendRedirect("registerSuccess.jsp");
层次结构 :
public interface UserService {
public abstract boolean exist(User u) throws Exception;
public abstract void add(User u)throws Exception;
}
public class UserServiceImpl implements UserService{
private UserDao userDao = new UserDaoImpl();
public UserDao getUserDao() {
return userDao;
}
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
public boolean exist(User u) {
return userDao.checkUserExistsWithName(u.getUsername());
}
}
public interface UserDao {
public boolean checkUserExistsWithName(String username);
}
public class UserDaoImpl implements UserDao {
public boolean checkUserExistsWithName(String username) {
SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
Session session = sessionFactory.getCurrentSession();
session.beginTransaction();
long count=(Long) session.createQuery("select count(*) from User u where u.username=:username")
.setString("username",username)
.uniqueResult();
session.getTransaction().commit();
if (count>0) return true;
return false;
}
}
是不是Hibernate版本的问题? 还是别的什么问题?
------解决方案--------------------
sessionFactory.getCurrentSession();别用这个,用openSession()
------解决方案--------------------
接一楼记得关闭哦亲
------解决方案--------------------
看错误的时候,不要只看错误第一行,下面的信息也很重要。