reset password
Author Message
hgadhia
Posts: 52
Posted 12:15 Feb 05, 2015 |

Hello,

I have the following Dao Implementation for user login,

@Override
public Users validateUser(Users user) {
            return entityManager.createQuery( "from Users where username = '" + user.getUsername() + "' and password = '"+user.getPassword()+"'", Users.class )
                .getSingleResult();
        
    }

This results in NoResultException exception when no match is found in database.

My question is, Shouldn't this return a null? Why is it throwing an exception?

Note: This is happening only when a match is not found in the database. Otherwise if a match is found, it returns a User object, and everything works fine.

Can someone please help?

 

 

Last edited by hgadhia at 12:47 Feb 05, 2015.
cysun
Posts: 2935
Posted 12:57 Feb 05, 2015 |

It's just how JPA API is designed.

hgadhia
Posts: 52
Posted 13:27 Feb 05, 2015 |
cysun wrote:

It's just how JPA API is designed.

So, does that mean that I should always use try/catch when I use entityManager to query database and manually return a null from the catch block?

The reason for asking this is that, I have not seen try/catch in the example code, so I got confused whether is there any work around to try/catch?

Last edited by hgadhia at 13:28 Feb 05, 2015.