空指针异常
Session session = MyHibernateSessionFactory.getSessionFactory().getCurrentSession();
这句一直报空指针异常
package service.impl;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import db.MyHibernateSessionFactory;
import entity.Users;
import service.UsersDao;
public class UsersDaoImpl implements UsersDao {
public boolean usersLogin(Users u) {
// 事务对象
Transaction tx = null;
String hql = "";
try {
Session session = MyHibernateSessionFactory.getSessionFactory().getCurrentSession();
tx = session.beginTransaction();
hql = "from Users where username=:username and password=:password ";
Query query = session.createQuery(hql);
query.setParameter(0, u.getUsername());
query.setParameter(1, u.getPassword());
List list = query.list();
tx.commit();// 提交事务
if (list.size() > 0) {
return true;
} else {
return false;
}
} catch (Exception ex) {
ex.printStackTrace();
return false;
} finally {
if (tx != null) {
// tx.commit();
tx = null;
}
}
}
}