Hql 子查询

直接上代码:

 public virtual IList<VoucherLog> GetMaxResultVoucherLog()
        {
            string orgaizationCode = HttpContext.Current.Session["OrganizationCode"].ToString();
            return this.Session.CreateQuery("select h from VoucherLog h where h.PeopleCode in (SELECT A.Code from  User A where A.organization.Id = :orgaizationCode) Order by h.Code desc")
             .SetString("orgaizationCode", orgaizationCode)
             .SetMaxResults(1)
             .List<VoucherLog>();
        }

在代码中我用in实现过滤子查询。在写hql语句的时候我们要特别注意,hql语句中出现的属性必须和对象中的属性对应上,如果是对象的话,要使用对象的关系如:

A.organization.Id  这就说明在User这个对象中还有一个带关系的organization对象,所有赋值的时候,应严格按照关系来走。
原文地址:https://www.cnblogs.com/gaolizhong/p/4429050.html