Bug in Hibernate session.createQuery().getSingleResult()

It seems I found a bug in Hibernate! Here it is:

    public User getUser(String email) throws UserNotFoundException {
        Session session = sessionFactory.getCurrentSession();
        Query<User> query = session.createQuery("from User as user where user.email=:email", User.class);

        query.setParameter("email", email);
        User user = query.getSingleResult();
        System.out.println("================================n" + user.getEmail() + "n===========");
        if (user == null)
            throw new UserNotFoundException();
        return user;

This method correctly find an ‘User’ object in database, prints email and doesn’t throws NullPointerException, but it also throws UserNotFoundException(this is my custom Exception)! So it seems like ‘user’ is a not-null object if there was not nullpointer and, on the other hand, it equals to null! Maybe there is a mistake in my thoughts?

Leave a Reply

Notify of