开发者

Noob question about hibernate criteria

I have a class called User which has 2 properties : login/password. I am trying to authenticate a user in my application using hibernate criteria but my request doesn't work.

[EDIT] The returned value is NULL. I have two users in my database for testing.

Here is my code :

@Override
public User authenticate(String login, String password)
    throws NullPointerException {
    Session session = this.getSession();
    User user = (User) session
        .createCriteria(User.class)
        .add(
                        Restrictions.and(
                            Property.forName("login").eq(login),
            开发者_StackOverflow                Property.forName("password").eq(password)
                    )).uniqueResult();

    if (user == null){
        throw new NullPointerException("User not found");
    }

    return user;
}

Can someone tells me what is wrong with my code?

Happy new Year 2011 !!


You should first retrieve the user from the database. If the user does not exist then show a message on your login page. All of your login names should be in unique column so there will be no need to press the uniqueResult restriction.

Second, once you got the user pulled up, you just compare the password hash and if it is OK let the user pass through.

The above will simplify your Hibernate logic and hence make your code easier to troubleshoot. This is how most sites have it done. At-least as far as I'm concerned.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜