Reputation: 225
I am trying to write hql query to retrieve the current session of a user using this approach
@Autowired
private SessionFactory _sessionFactory;
private Session getSession() {
return _sessionFactory.getCurrentSession();
}
I am trying to achieve something like this
return getSession().createQuery("SELECT FROM Users u WHERE u.id = :userIdFromSession AND u.isAdmin = true").list();
this is my approach
return getSession().createQuery("SELECT FROM Users u WHERE u.id = " +:getSession() +"AND u.isAdmin = true").list();
assuming the getters and setters are in place this is the user entity class
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "userId")
private Long id;
@Column(nullable = false)
private String name;
@Column(unique = true, nullable = false)
private String email;
@Column(nullable = false)
private long timestamp;
@OneToOne
@JoinColumn(name = "adminId",nullable = false)
Admin admin;
@Column(nullable = true)
private boolean isAdmin;
assuming getters and setters, this is the chat entity class
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "chatId")
private Long id;
private String message;
@OneToOne
@JoinColumn(name = "userId")
User user;
now you can see that chat references user. you must be a user before you can chat, so I want to retrieve the chat of a user this way in dao class
// return getSession().createQuery("SELECT FROM Chats u WHERE u.id = :userIdFromSession AND u.isAdmin = true").list();
this is the method I am using to create a user in my controller class
@RequestMapping(value = "/create-user", method = RequestMethod.POST)
public ModelAndView createUser(HttpServletRequest request,
HttpServletResponse response,
@RequestParam String name,
@RequestParam String email) {
try {
// create new user object
User user = new User();
user.setName(name);
user.setEmail(email);
user.setTimestamp(new Date().getTime());
// save user in db (if new)
if (_userDao.getByEmail(email) == null) {
_userDao.save(user);
}
// save in cookie
Cookie cookie = new Cookie("name", name);
Cookie cookie1 = new Cookie("email", email);
response.addCookie(cookie);
response.addCookie(cookie1);
} catch (Exception e) {
e.printStackTrace();
//logger.error("Exception in creating user: ", e.getStackTrace());
}
return new ModelAndView("redirect:/");
}
this approach is not just working out as it throwing compilation error. Kindly assist!
Upvotes: 3
Views: 5526
Reputation: 2664
The Hibernate Session has nothing to do with the user's session. You can think of it in terms of connection to the database.
SessionFactory#getCurrentSession()
returns the current session if one is already open. Through it, you can query the database.
The way I understand your problem now, is to identify the Spring MVC controllers that will deal with authentication. Your createUser() method may be one of them, but surely you will also need an authenticateUser() somewhere. Let's pretend email are unique and you use the email as a login to authenticate users. Let's suppose that your getByEmail method returns a User POJO. I'll leave exception handling and Spring MVC stuff out.
public ModelAndView authenficateUser(HttpServletRequest request,
HttpServletResponse response,
@RequestParam String email) {
User u = _userDao.getByEmail(email);
if (u == null) {
// deal with unknown user and return to login form
} else {
request.getSession().setAttribute("email", email);
// success
}
}
Then you should have a getChats method in a controller somewhere.
public ModelAndView getChats(HttpServletRequest request,
HttpServletResponse response) {
String email = (String) request.getSession().getAttribute("email");
if (email == null) {
// user is not logged in, deal with it.
} else {
List<Chat> chats = _chatDao.getChats(email);
return new ModelAndView("chatView", "chats", chats);
}
}
In ChatDao, you have a method like this:
List<Chat> getChats(String email) {
String hql = "from Chat c where c.user.email = :email";
_sessionFactory.getCurrentSession().createQuery(hql).setParameter("email", email).list();
}
Upvotes: 2