-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 
Author Message
 Post subject: UserDAO::getUserById(Long userId)
PostPosted: Mon Mar 21, 2005 5:56 pm 
Beginner
Beginner

Joined: Fri Mar 12, 2004 5:18 pm
Posts: 44
Hi,

In the 'Hibernate In Action' example page 309, it creates a DAO object (UserDAO) for User lookup. The method getUserById() takes an userId as its input parameter.

My question is in real world user, isn't we often need to find the user based on his/her username, not the id in the database table? This is because user will enter his/her username (not the id generated by database) during login.

So how can I do the getUserById() using the username instead of the database ID?

I think of using HQL to query for the user with the username that I am looking for. But i can't load it the way I can with getUserById().

Is there a better solution?

Thank you.



Code:

public User getUserById(Long userId, boolean lock)
         throws InfrastructureException {

      Session session = HibernateUtil.getSession();
      User user = null;
      try {
         if (lock) {
            user = (User) session.load(User.class, userId, LockMode.UPGRADE);
         } else {
            user = (User) session.load(User.class, userId);
         }
      }  catch (HibernateException ex) {
         throw new InfrastructureException(ex);
      }
      return user;
   }


public User getUserByName(String name)
       throws HibernateException {
       Session session = HibernateUtil.getSession();
       Query q = session.createQuery("select c from User as c where c.name = :name");
       q.setString("name", name);


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 21, 2005 5:58 pm 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
Of course, write a query which searches by login name. There is a whole chapter in the book explaining queries.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 21, 2005 6:52 pm 
Beginner
Beginner

Joined: Fri Mar 12, 2004 5:18 pm
Posts: 44
Thanks Christian,

But how can I *lock* the User like this if I get the User object using query?

this is from the getUserById() code:
session.load(User.class, userId, LockMode.UPGRADE);


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 21, 2005 7:08 pm 
Beginner
Beginner

Joined: Fri Mar 12, 2004 5:18 pm
Posts: 44
I think I find the answer.
Thanks.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.