java

How to access SessionFactory from spring-dispatcher-servlet.xml to DAO  #angularjs #reactjs

  • public class UserService { User user; UserDao dao; public boolean authenticate(String username, String password) { user = new User(); user.setUsername(username); user.setPassword(password.toCharArray()); if(dao.authenticateUser(user)) { return true; } return false; } }
  • String username, @RequestParam(“password”)String password){ userService = new UserService(); boolean flag = userService.authenticate(username,password); if(flag){ ModelAndView modelAndView = new ModelAndView(“login”); return modelAndView; } else{ ModelAndView modelAndView = new ModelAndView(“wrong”); return modelAndView; } } }
  • java spring hibernate spring-mvc
  • = null) { return true; } return false; } }
  • @Repository public class UserDao implements IUser { @Autowired SessionFactory sessionFactory; public SessionFactory getSessionFactory() { return sessionFactory; } public void setSessionFactory(SessionFactory sessionFactory) { this.sessionFactory = sessionFactory; } @Override public boolean authenticateUser(User user) { String username = user.getUsername(); char[] password = user.getPassword(); System.out.println(username +” <----> “+password); String hql = “from User where username=’username’ and password =’password'”; Query query = getSessionFactory().openSession().createQuery(hql); List list = new ArrayList(); list = query.list(); if (list.size() > 0 && list !

I am creating a spring web application with help of hibernate. I have created spring-dipatcher-servlet.xml for all the configuration. I want to access the database using hibernate without creating the hibernate.cfg.xml file as I am using spring. I am getting Null Pointer Exception while accessing the Session Factory in DAO.

@ng_real_ninja: How to access SessionFactory from spring-dispatcher-servlet.xml to DAO #angularjs #reactjs

Following are the snippets of what I have done till now.

@Controller public class QuizMcqController { UserService userService; @RequestMapping(value=”/welcome.htm”) public ModelAndView redirectToLoginPage(){ ModelAndView modelAndView = new ModelAndView(“login”); return modelAndView; } @RequestMapping(value=”/AuthenticateUser.htm”, method = RequestMethod.POST) public ModelAndView authenticateUser(@RequestParam(“username”) String username, @RequestParam(“password”)String password){ userService = new UserService(); boolean flag = userService.authenticate(username,password); if(flag){ ModelAndView modelAndView = new ModelAndView(“login”); return modelAndView; } else{ ModelAndView modelAndView = new ModelAndView(“wrong”); return modelAndView; } } }

public class UserService { User user; UserDao dao; public boolean authenticate(String username, String password) { user = new User(); user.setUsername(username); user.setPassword(password.toCharArray()); if(dao.authenticateUser(user)) { return true; } return false; } }

—DAO —

@Repository public class UserDao implements IUser { @Autowired SessionFactory sessionFactory; public SessionFactory getSessionFactory() { return sessionFactory; } public void setSessionFactory(SessionFactory sessionFactory) { this.sessionFactory = sessionFactory; } @Override public boolean authenticateUser(User user) { String username = user.getUsername(); char[] password = user.getPassword(); System.out.println(username +” <----> “+password); String hql = “from User where username=’username’ and password =’password'”; Query query = getSessionFactory().openSession().createQuery(hql); List list = new ArrayList(); list = query.list(); if (list.size() > 0 && list != null) { return true; } return false; } }

How can I resolve my issue, what I am doing wrong?

Thanks in advance. Your help is appreciated.

java

You might also like More from author

Comments are closed, but trackbacks and pingbacks are open.