问答详情
源自:2-3 用户业务逻辑接口和类

空指针异常

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;

}

}

}

}


提问者:qq_雙子_04312075 2019-06-28 16:50

个回答

  • qq_雙子_04312075
    2019-06-28 17:37:30

    还是自己太粗心,MyHibernateSessionFactory.java中初始化sessionFactory时缺少一句代码sessionFactory = config.buildSessionFactory(serviceRegistry);,原先我直接就返回对象了,跟着敲代码眼睛不能一直看着视频,自己写完后还是要仔细比对检查下。