如果查询单个对象,Spring无法访问数据库数据

时间:2022-12-08 13:06:49

I'm trying to query a single object from the database and whenever I execute the app it throws a null pointer exception, but when I query the whole table and place it on a list, I am getting all the result so there is no problem with the configurations for I can access the database. here is the part of the code that throws error:

我正在尝试从数据库中查询单个对象,每当我执行应用程序时它会抛出一个空指针异常,但是当我查询整个表并将其放在列表中时,我得到了所有结果,所以没有问题配置我可以访问数据库。这是抛出错误的代码的一部分:

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.query.Query;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.erp.entity.User;

@Repository
public class UsersDAOImpl implements UsersDAO {

@Autowired
private SessionFactory sessionFactory;

@Override
@Transactional
public User getUser(String username) {
    Session currentSession = sessionFactory.getCurrentSession();

    String hql = "FROM User WHERE username = '" + username + "'";

    Query<User> query =  currentSession.createQuery(hql, User.class);

    User user = (User) query.getSingleResult();

    return user;
}

@Override
@Transactional
public List<User> getUsers() {
    Session currentSession = sessionFactory.getCurrentSession();

    Query<User> query = currentSession.createQuery("FROM User", User.class);

    List<User> users = query.getResultList();

    return users;
}

}

Here is the thrown error:

这是抛出的错误:

Servlet.service() for servlet [dispatcher] in context with path [/ERPapplication] threw exception[Request processing failed; nested exception is java.lang.NullPointerException] with root cause java.lang.NullPointerException at com.erp.dao.UsersDAOImpl.getUser(UsersDAOImpl.java:23) at com.erp.controller.LoginController.greeting(LoginController.java:39)

Here is the controller class:

这是控制器类:

@Controller
@RequestMapping("/erp")
public class LoginController {

@Autowired
private UsersDAO usersDAO;

@RequestMapping("/list")
public String listUsers(Model model) {
    List<User> users = usersDAO.getUsers();

    model.addAttribute("users", users);

    return "list-users";
}

@RequestMapping("/login")
public String login(Model model) {
    return "login";
}

@RequestMapping("/test")
public String greeting(Model model) {
    UsersDAO usersDAO = new UsersDAOImpl();

    if(usersDAO.getUser("username").getUsername().equals("username")) {
        return "greeting";
    } else {
        return "list-users";
    }
}
}

1 个解决方案

#1


1  

You are using UsersDAO usersDAO = new UsersDAOImpl(); in your Controller class. Thats a new instance, not the singleton created with Spring component scan. Use the Autowired one, UsersDao usersDao to call the method. I think you didn't notice that!

您正在使用UsersDAO usersDAO = new UsersDAOImpl();在您的Controller类中。这是一个新实例,而不是使用Spring组件扫描创建的单例。使用Autowired one,UsersDao usersDao来调用该方法。我想你没注意到!

#1


1  

You are using UsersDAO usersDAO = new UsersDAOImpl(); in your Controller class. Thats a new instance, not the singleton created with Spring component scan. Use the Autowired one, UsersDao usersDao to call the method. I think you didn't notice that!

您正在使用UsersDAO usersDAO = new UsersDAOImpl();在您的Controller类中。这是一个新实例,而不是使用Spring组件扫描创建的单例。使用Autowired one,UsersDao usersDao来调用该方法。我想你没注意到!