views:

200

answers:

2

Any one who tell me the query using criteria/hql/sql. Requirement is that user enter email or username the query return the password of the user from table user.

+1  A: 

If all you're doing is fetching one field, you probably just want to go hql (or possibly sql).

If you do criteria, I believe you're pulling back the entire object, just to eventually use one field.

Edit: That's a really broad question. Here is a tutorial

taylonr
yes i want pull back the entire object using one field from email or username. but i donot know how??????
ali
I edited to include a tutorial... asking how to do nhibernate is probably a bit out of scope here. If you have specific questions, those can be addressed easier.
taylonr
this is the query i want the user enter email or username and the query returns user......now the problem is OR condition where i put or condition.......
ali
+2  A: 

The Criteria API is very appropriate for dynamic query generation and would have my preference here. You could do something like this:

Criteria criteria = session.createCriteria(User.class)
    .setProjection(Projections.property("password"));

if (email != null) {
    criteria.add(Expression.eq("email", email));
}
if (username != null) {
    criteria.add(Expression.eq("username", username));
}
String password = (String) criteria.uniqueResult();

Note that I'm a bit extrapolating but you shouldn't store clear passwords in database and you shouldn't send passwords by email (which is unsecure by nature). Actually, a common procedure for password recovery is to send a link with a limited lifetime by mail allowing the user to enter a new password.


Update: Actually, you may not need a dynamic query here but I'm leaving the above for reference.

To implement an OR with the Criteria API, you can do something like this:

Criteria criteria = session.createCriteria(User.class);
Criterion username = Restrictions.eq("username", usernameOrPassword);
Criterion email = Restrictions.eq("email", usernameOrPassword);
LogicalExpression orExp = Restrictions.or(username, email);
criteria.add(orExp);

In HQL, you could run the following query:

from User s 
where u.username = :usernameOrPassword 
   or u.password = :usernameOrPassword

In this case, it doesn't matter which solution you choose, both will do the job.

Pascal Thivent
thanks alot for quick response Mr Pascal.
ali
`if(email==null) criteria.add(Expression.isNull("email"))` etc.?
Alison
@Alison: Hmm... What? What I mean in my answer is: "if the email parameter is not null, then add it in the where clause. Same for the username". This is very different from what you wrote which is not what the OP wants IMO.
Pascal Thivent
public RegularUser findByUserNameOrEmailId(String userNameOrEmailId) { List<RegularUser> list=super.findByCriteria(Restrictions.eq("username",userNameOrEmailId), Restrictions.eq("password", userNameOrEmailId)); return list.size()>0 ? list.get(0) : null; }
ali
the user give only one perameter
ali
i want or condition between username and email from both any criteria meet the query return regularuser
ali
again thanks Mr Pascal.It is first time that i am here stackoverflow.i am very happy to join this.Again thanks Mr Pascal........
ali
@Pascal I think you're correct, and I misunderstood - sorry for the confusion
Alison
@Alison: No problem. And you're welcome.
Pascal Thivent