views:

64

answers:

2

I have a user class:

public class User
{
   public virtual int ID {get;set;}
   public virtual string UserGuid {get;set;}  // its unique!
}

Can someone show me how to query using HQL and criteria to get the user by UserGuid?

+1  A: 

if it's unique :

var query = Session.CreateQuery("from User u where u.UserGuid = :id");
            query.SetString("id", id);

var user = query.UniqueResult<User>();
Matthieu
+1  A: 

Matthieu gave you the HQL here's the Criteria:

public User GetUserByUserGuid(Guid userGuid){

  ICriteria crit = Session.CreateCriteria<User>();
  crit.Add(Expression.Eq("UserGuid", userGuid));
  return crit.UniqueResult<User>(); //will return null if not found

}
Michael Gattuso