且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Castle ActiveRecord学习实践(7):使用HQL查询

更新时间:2021-11-08 00:08:35

摘要:虽然ActiveRecord为我们提供了Find()FindAll()这样两个静态的查询方法,并且有Where特性可供使用,但是仍然不能解决实际开发中一些复杂的查询,这时我们就需要通过HQL查询来实现。
 
主要内容
1HQL概述
2SimpleQuery查询
3ScalarQuery查询
4.自定义查询
5.使用CallBack
 
一.HQL简单介绍
HQL全名是Hibernate Query Language,它是一种完全面向对象的查询语言。先来看一下HQL最基本的一些用法
1From子句
Castle ActiveRecord学习实践(7):使用HQL查询from Post
你也可以为Post起一个别名
Castle ActiveRecord学习实践(7):使用HQL查询from Post as post
或者省略as
Castle ActiveRecord学习实践(7):使用HQL查询from Post post
2Select 子句
Castle ActiveRecord学习实践(7):使用HQL查询select Name,Author from Blog
也可以使用elements函数来查询一个集合
Castle ActiveRecord学习实践(7):使用HQL查询select elements(blog.Posts) from Blog blog
3.使用聚合函数
HQL中也可以使用一些聚合函数
Castle ActiveRecord学习实践(7):使用HQL查询select count(*from Blog blog
 
Castle ActiveRecord学习实践(7):使用HQL查询select count(elements(blog.Posts)) from Blog blog
HQL支持的聚合函数有
Castle ActiveRecord学习实践(7):使用HQL查询avg(Castle ActiveRecord学习实践(7):使用HQL查询), sum(Castle ActiveRecord学习实践(7):使用HQL查询), min(Castle ActiveRecord学习实践(7):使用HQL查询), max(Castle ActiveRecord学习实践(7):使用HQL查询
Castle ActiveRecord学习实践(7):使用HQL查询
Castle ActiveRecord学习实践(7):使用HQL查询
count(*
Castle ActiveRecord学习实践(7):使用HQL查询
Castle ActiveRecord学习实践(7):使用HQL查询
count(Castle ActiveRecord学习实践(7):使用HQL查询), count(distinct Castle ActiveRecord学习实践(7):使用HQL查询), count(allCastle ActiveRecord学习实践(7):使用HQL查询)
4Where子句
Castle ActiveRecord学习实践(7):使用HQL查询from Blog blog where blog.Name = ‘Terry Lee’
 
Castle ActiveRecord学习实践(7):使用HQL查询from Blog blog where blog.Name is not null
详细可以参考[url]http://www.hibernate.org/hib_docs/reference/en/html/queryhql.html[/url]
 
二.SimpleQuery查询
SimpleQuery是一种最简单的查询,它直接处理HQL语句,并返回一个集合,没有复杂的参数处理。具体用法可以参考下例:
Castle ActiveRecord学习实践(7):使用HQL查询[ActiveRecord("Posts")]
Castle ActiveRecord学习实践(7):使用HQL查询
Castle ActiveRecord学习实践(7):使用HQL查询
public class Post : ActiveRecordBase
Castle ActiveRecord学习实践(7):使用HQL查询
Castle ActiveRecord学习实践(7):使用HQL查询
{
Castle ActiveRecord学习实践(7):使用HQL查询
Castle ActiveRecord学习实践(7):使用HQL查询    
// Castle ActiveRecord学习实践(7):使用HQL查询Castle ActiveRecord学习实践(7):使用HQL查询
Castle ActiveRecord学习实践(7):使用HQL查询

Castle ActiveRecord学习实践(7):使用HQL查询    
/// <summary>
Castle ActiveRecord学习实践(7):使用HQL查询
Castle ActiveRecord学习实践(7):使用HQL查询    
/// 查询某一类别的所有Posts
Castle ActiveRecord学习实践(7):使用HQL查询
Castle ActiveRecord学习实践(7):使用HQL查询    
/// </summary>
Castle ActiveRecord学习实践(7):使用HQL查询
Castle ActiveRecord学习实践(7):使用HQL查询    
/// <param name="_strCategory">类别名称</param>
Castle ActiveRecord学习实践(7):使用HQL查询
Castle ActiveRecord学习实践(7):使用HQL查询    
/// <returns></returns>

Castle ActiveRecord学习实践(7):使用HQL查询
Castle ActiveRecord学习实践(7):使用HQL查询    
public static Post[] GetPostsByCategory(string _strCategory)
Castle ActiveRecord学习实践(7):使用HQL查询
Castle ActiveRecord学习实践(7):使用HQL查询    
{
Castle ActiveRecord学习实践(7):使用HQL查询
Castle ActiveRecord学习实践(7):使用HQL查询        SimpleQuery query 
= new SimpleQuery(
Castle ActiveRecord学习实践(7):使用HQL查询
Castle ActiveRecord学习实践(7):使用HQL查询            
typeof(Post),
Castle ActiveRecord学习实践(7):使用HQL查询
Castle ActiveRecord学习实践(7):使用HQL查询            
@"from Post post where post.Category = ?",
Castle ActiveRecord学习实践(7):使用HQL查询
Castle ActiveRecord学习实践(7):使用HQL查询            _strCategory
Castle ActiveRecord学习实践(7):使用HQL查询
Castle ActiveRecord学习实践(7):使用HQL查询            );
Castle ActiveRecord学习实践(7):使用HQL查询
Castle ActiveRecord学习实践(7):使用HQL查询 
Castle ActiveRecord学习实践(7):使用HQL查询
Castle ActiveRecord学习实践(7):使用HQL查询        
return (Post[])ExecuteQuery(query);
Castle ActiveRecord学习实践(7):使用HQL查询
Castle ActiveRecord学习实践(7):使用HQL查询    }

Castle ActiveRecord学习实践(7):使用HQL查询
Castle ActiveRecord学习实践(7):使用HQL查询    
Castle ActiveRecord学习实践(7):使用HQL查询
Castle ActiveRecord学习实践(7):使用HQL查询    
/// <summary>
Castle ActiveRecord学习实践(7):使用HQL查询
Castle ActiveRecord学习实践(7):使用HQL查询    
/// 查询某一时间段内发表的所有Posts
Castle ActiveRecord学习实践(7):使用HQL查询
Castle ActiveRecord学习实践(7):使用HQL查询    
/// </summary>
Castle ActiveRecord学习实践(7):使用HQL查询
Castle ActiveRecord学习实践(7):使用HQL查询    
/// <param name="start">开始时间</param>
Castle ActiveRecord学习实践(7):使用HQL查询
Castle ActiveRecord学习实践(7):使用HQL查询    
/// <param name="end">结束时间</param>
Castle ActiveRecord学习实践(7):使用HQL查询
Castle ActiveRecord学习实践(7):使用HQL查询    
/// <returns></returns>

Castle ActiveRecord学习实践(7):使用HQL查询
Castle ActiveRecord学习实践(7):使用HQL查询    
public static int[] GetPostsInterval(DateTime start,DateTime end)
Castle ActiveRecord学习实践(7):使用HQL查询
Castle ActiveRecord学习实践(7):使用HQL查询    
{
Castle ActiveRecord学习实践(7):使用HQL查询
Castle ActiveRecord学习实践(7):使用HQL查询        SimpleQuery query 
= new SimpleQuery(
Castle ActiveRecord学习实践(7):使用HQL查询
Castle ActiveRecord学习实践(7):使用HQL查询            
typeof(Post),typeof(int),
Castle ActiveRecord学习实践(7):使用HQL查询
Castle ActiveRecord学习实践(7):使用HQL查询            
@"select post.Id from Post post where post.Created between ? and ?",
Castle ActiveRecord学习实践(7):使用HQL查询
Castle ActiveRecord学习实践(7):使用HQL查询            start,end
Castle ActiveRecord学习实践(7):使用HQL查询
Castle ActiveRecord学习实践(7):使用HQL查询            );
Castle ActiveRecord学习实践(7):使用HQL查询
Castle ActiveRecord学习实践(7):使用HQL查询 
Castle ActiveRecord学习实践(7):使用HQL查询
Castle ActiveRecord学习实践(7):使用HQL查询        
return (int[])ExecuteQuery(query);
Castle ActiveRecord学习实践(7):使用HQL查询
Castle ActiveRecord学习实践(7):使用HQL查询    }

Castle ActiveRecord学习实践(7):使用HQL查询
Castle ActiveRecord学习实践(7):使用HQL查询}

看一下简单的测试代码:
Castle ActiveRecord学习实践(7):使用HQL查询[Test]
Castle ActiveRecord学习实践(7):使用HQL查询
Castle ActiveRecord学习实践(7):使用HQL查询
public void TestGetPostsByCategory()
Castle ActiveRecord学习实践(7):使用HQL查询
Castle ActiveRecord学习实践(7):使用HQL查询
{
Castle ActiveRecord学习实践(7):使用HQL查询
Castle ActiveRecord学习实践(7):使用HQL查询    
string StrCategory = "Castle";
Castle ActiveRecord学习实践(7):使用HQL查询
Castle ActiveRecord学习实践(7):使用HQL查询    
Castle ActiveRecord学习实践(7):使用HQL查询
Castle ActiveRecord学习实践(7):使用HQL查询    IList list 
= (IList)Post.GetPostsByCategory(StrCategory);
Castle ActiveRecord学习实践(7):使用HQL查询
Castle ActiveRecord学习实践(7):使用HQL查询 
Castle ActiveRecord学习实践(7):使用HQL查询
Castle ActiveRecord学习实践(7):使用HQL查询    
int expectedCount = 2;
Castle ActiveRecord学习实践(7):使用HQL查询
Castle ActiveRecord学习实践(7):使用HQL查询 
Castle ActiveRecord学习实践(7):使用HQL查询
Castle ActiveRecord学习实践(7):使用HQL查询    Assert.IsNotNull(list);
Castle ActiveRecord学习实践(7):使用HQL查询
Castle ActiveRecord学习实践(7):使用HQL查询    Assert.AreEqual(expectedCount,list.Count);
Castle ActiveRecord学习实践(7):使用HQL查询
Castle ActiveRecord学习实践(7):使用HQL查询}

Castle ActiveRecord学习实践(7):使用HQL查询
Castle ActiveRecord学习实践(7):使用HQL查询 
Castle ActiveRecord学习实践(7):使用HQL查询
Castle ActiveRecord学习实践(7):使用HQL查询[Test]
Castle ActiveRecord学习实践(7):使用HQL查询
Castle ActiveRecord学习实践(7):使用HQL查询
public void TestGetPostsInterval()
Castle ActiveRecord学习实践(7):使用HQL查询
Castle ActiveRecord学习实践(7):使用HQL查询
{
Castle ActiveRecord学习实践(7):使用HQL查询
Castle ActiveRecord学习实践(7):使用HQL查询    DateTime start 
= Convert.ToDateTime("2006-01-01");
Castle ActiveRecord学习实践(7):使用HQL查询
Castle ActiveRecord学习实践(7):使用HQL查询    DateTime end 
= DateTime.Now;
Castle ActiveRecord学习实践(7):使用HQL查询
Castle ActiveRecord学习实践(7):使用HQL查询 
Castle ActiveRecord学习实践(7):使用HQL查询
Castle ActiveRecord学习实践(7):使用HQL查询    IList list 
= (IList)Post.GetPostsInterval(start,end);
Castle ActiveRecord学习实践(7):使用HQL查询
Castle ActiveRecord学习实践(7):使用HQL查询 
Castle ActiveRecord学习实践(7):使用HQL查询
Castle ActiveRecord学习实践(7):使用HQL查询    
int expectedCount = 2;
Castle ActiveRecord学习实践(7):使用HQL查询
Castle ActiveRecord学习实践(7):使用HQL查询 
Castle ActiveRecord学习实践(7):使用HQL查询
Castle ActiveRecord学习实践(7):使用HQL查询    Assert.IsNotNull(list);
Castle ActiveRecord学习实践(7):使用HQL查询
Castle ActiveRecord学习实践(7):使用HQL查询    Assert.AreEqual(expectedCount,list.Count);
Castle ActiveRecord学习实践(7):使用HQL查询
Castle ActiveRecord学习实践(7):使用HQL查询}
 
三.ScalarQuery查询
ScalarQuery查询也是一种简单的直接处理HQL的查询,它也没有复杂的参数处理,只不过返回的值不是集合而是单一的值,具体用法参考下例:
Castle ActiveRecord学习实践(7):使用HQL查询[ActiveRecord("Blogs")]
Castle ActiveRecord学习实践(7):使用HQL查询
Castle ActiveRecord学习实践(7):使用HQL查询
public class Blog : ActiveRecordBase
Castle ActiveRecord学习实践(7):使用HQL查询
Castle ActiveRecord学习实践(7):使用HQL查询
{
Castle ActiveRecord学习实践(7):使用HQL查询
Castle ActiveRecord学习实践(7):使用HQL查询    
// Castle ActiveRecord学习实践(7):使用HQL查询Castle ActiveRecord学习实践(7):使用HQL查询
Castle ActiveRecord学习实践(7):使用HQL查询

Castle ActiveRecord学习实践(7):使用HQL查询    
/// <summary>
Castle ActiveRecord学习实践(7):使用HQL查询
Castle ActiveRecord学习实践(7):使用HQL查询    
/// 查询某作者发表的所有Posts数量
Castle ActiveRecord学习实践(7):使用HQL查询
Castle ActiveRecord学习实践(7):使用HQL查询    
/// </summary>
Castle ActiveRecord学习实践(7):使用HQL查询
Castle ActiveRecord学习实践(7):使用HQL查询    
/// <param name="_StrAuthor">作者姓名</param>
Castle ActiveRecord学习实践(7):使用HQL查询
Castle ActiveRecord学习实践(7):使用HQL查询    
/// <returns></returns>

Castle ActiveRecord学习实践(7):使用HQL查询
Castle ActiveRecord学习实践(7):使用HQL查询    
public static int GetPostNumByAuthor(string _StrAuthor)
Castle ActiveRecord学习实践(7):使用HQL查询
Castle ActiveRecord学习实践(7):使用HQL查询    
{
Castle ActiveRecord学习实践(7):使用HQL查询
Castle ActiveRecord学习实践(7):使用HQL查询        ScalarQuery query 
= new ScalarQuery(
Castle ActiveRecord学习实践(7):使用HQL查询
Castle ActiveRecord学习实践(7):使用HQL查询            
typeof(Blog),
Castle ActiveRecord学习实践(7):使用HQL查询
Castle ActiveRecord学习实践(7):使用HQL查询            
@"select count(elements(blog.Posts)) from Blog blog where blog.Author = ?",
Castle ActiveRecord学习实践(7):使用HQL查询
Castle ActiveRecord学习实践(7):使用HQL查询            _StrAuthor
Castle ActiveRecord学习实践(7):使用HQL查询
Castle ActiveRecord学习实践(7):使用HQL查询            );
Castle ActiveRecord学习实践(7):使用HQL查询
Castle ActiveRecord学习实践(7):使用HQL查询 
Castle ActiveRecord学习实践(7):使用HQL查询
Castle ActiveRecord学习实践(7):使用HQL查询        
return (int)ExecuteQuery(query);
Castle ActiveRecord学习实践(7):使用HQL查询
Castle ActiveRecord学习实践(7):使用HQL查询    }

Castle ActiveRecord学习实践(7):使用HQL查询
Castle ActiveRecord学习实践(7):使用HQL查询}

看一下简单的测试代码
Castle ActiveRecord学习实践(7):使用HQL查询[Test]
Castle ActiveRecord学习实践(7):使用HQL查询
Castle ActiveRecord学习实践(7):使用HQL查询
public void TestGetPostNumByAuthor()
Castle ActiveRecord学习实践(7):使用HQL查询
Castle ActiveRecord学习实践(7):使用HQL查询
{
Castle ActiveRecord学习实践(7):使用HQL查询
Castle ActiveRecord学习实践(7):使用HQL查询    
string _StrAuthor = "Terry Lee";
Castle ActiveRecord学习实践(7):使用HQL查询
Castle ActiveRecord学习实践(7):使用HQL查询 
Castle ActiveRecord学习实践(7):使用HQL查询
Castle ActiveRecord学习实践(7):使用HQL查询    
int result = Blog.GetPostNumByAuthor(_StrAuthor);
Castle ActiveRecord学习实践(7):使用HQL查询
Castle ActiveRecord学习实践(7):使用HQL查询 
Castle ActiveRecord学习实践(7):使用HQL查询
Castle ActiveRecord学习实践(7):使用HQL查询    
int expectedCount = 2;
Castle ActiveRecord学习实践(7):使用HQL查询
Castle ActiveRecord学习实践(7):使用HQL查询 
Castle ActiveRecord学习实践(7):使用HQL查询
Castle ActiveRecord学习实践(7):使用HQL查询    Assert.AreEqual(expectedCount,result);
Castle ActiveRecord学习实践(7):使用HQL查询
Castle ActiveRecord学习实践(7):使用HQL查询}

四.自定义查询
在实际开发中,我们所面对的查询远不止上面所说得这么简单,有时候我们需要处理一些自定义的参数,或者执行自定义的查询语句,这时需要我们编写自定义的ActiveRecord查询,首先要添加一个类,让它继承于基类ActiveRecordBaseQuery,并覆写Execute()方法(或者实现IactiveRecordQuery接口),如下例所示
Castle ActiveRecord学习实践(7):使用HQL查询public class QueryWithNamedParameters : ActiveRecordBaseQuery
Castle ActiveRecord学习实践(7):使用HQL查询
Castle ActiveRecord学习实践(7):使用HQL查询
{
Castle ActiveRecord学习实践(7):使用HQL查询
Castle ActiveRecord学习实践(7):使用HQL查询    
private string _authorName = null;
Castle ActiveRecord学习实践(7):使用HQL查询
Castle ActiveRecord学习实践(7):使用HQL查询    
private int _maxResults = 2;
Castle ActiveRecord学习实践(7):使用HQL查询
Castle ActiveRecord学习实践(7):使用HQL查询    
Castle ActiveRecord学习实践(7):使用HQL查询
Castle ActiveRecord学习实践(7):使用HQL查询    
public QueryWithNamedParameters()
Castle ActiveRecord学习实践(7):使用HQL查询
Castle ActiveRecord学习实践(7):使用HQL查询        : 
base(typeof(Blog)) 
Castle ActiveRecord学习实践(7):使用HQL查询
Castle ActiveRecord学习实践(7):使用HQL查询    
{
Castle ActiveRecord学习实践(7):使用HQL查询
Castle ActiveRecord学习实践(7):使用HQL查询 
Castle ActiveRecord学习实践(7):使用HQL查询
Castle ActiveRecord学习实践(7):使用HQL查询    }

Castle ActiveRecord学习实践(7):使用HQL查询
Castle ActiveRecord学习实践(7):使用HQL查询    
Castle ActiveRecord学习实践(7):使用HQL查询
Castle ActiveRecord学习实践(7):使用HQL查询    
public string AuthorName
Castle ActiveRecord学习实践(7):使用HQL查询
Castle ActiveRecord学习实践(7):使用HQL查询    
{
Castle ActiveRecord学习实践(7):使用HQL查询
Castle ActiveRecord学习实践(7):使用HQL查询        
get return _authorName; }
Castle ActiveRecord学习实践(7):使用HQL查询
Castle ActiveRecord学习实践(7):使用HQL查询        
set { _authorName = value; }
Castle ActiveRecord学习实践(7):使用HQL查询
Castle ActiveRecord学习实践(7):使用HQL查询    }

Castle ActiveRecord学习实践(7):使用HQL查询
Castle ActiveRecord学习实践(7):使用HQL查询    
Castle ActiveRecord学习实践(7):使用HQL查询
Castle ActiveRecord学习实践(7):使用HQL查询    
public int MaxResults
Castle ActiveRecord学习实践(7):使用HQL查询
Castle ActiveRecord学习实践(7):使用HQL查询    
{
Castle ActiveRecord学习实践(7):使用HQL查询
Castle ActiveRecord学习实践(7):使用HQL查询        
get return _maxResults; }
Castle ActiveRecord学习实践(7):使用HQL查询
Castle ActiveRecord学习实践(7):使用HQL查询        
set { _maxResults = value; }
Castle ActiveRecord学习实践(7):使用HQL查询
Castle ActiveRecord学习实践(7):使用HQL查询    }

Castle ActiveRecord学习实践(7):使用HQL查询
Castle ActiveRecord学习实践(7):使用HQL查询 
Castle ActiveRecord学习实践(7):使用HQL查询
Castle ActiveRecord学习实践(7):使用HQL查询    
public override object Execute(ISession session)
Castle ActiveRecord学习实践(7):使用HQL查询
Castle ActiveRecord学习实践(7):使用HQL查询    
{
Castle ActiveRecord学习实践(7):使用HQL查询
Castle ActiveRecord学习实践(7):使用HQL查询        String hql 
= "from Blog blog";
Castle ActiveRecord学习实践(7):使用HQL查询
Castle ActiveRecord学习实践(7):使用HQL查询 
Castle ActiveRecord学习实践(7):使用HQL查询
Castle ActiveRecord学习实践(7):使用HQL查询        
if (_authorName != null)
Castle ActiveRecord学习实践(7):使用HQL查询
Castle ActiveRecord学习实践(7):使用HQL查询            hql 
+= " where blog.Author = :author";
Castle ActiveRecord学习实践(7):使用HQL查询
Castle ActiveRecord学习实践(7):使用HQL查询 
Castle ActiveRecord学习实践(7):使用HQL查询
Castle ActiveRecord学习实践(7):使用HQL查询        IQuery q 
= session.CreateQuery(hql);
Castle ActiveRecord学习实践(7):使用HQL查询
Castle ActiveRecord学习实践(7):使用HQL查询 
Castle ActiveRecord学习实践(7):使用HQL查询
Castle ActiveRecord学习实践(7):使用HQL查询        
if (_authorName != null)
Castle ActiveRecord学习实践(7):使用HQL查询
Castle ActiveRecord学习实践(7):使用HQL查询            q.SetString(
"author", _authorName);
Castle ActiveRecord学习实践(7):使用HQL查询
Castle ActiveRecord学习实践(7):使用HQL查询 
Castle ActiveRecord学习实践(7):使用HQL查询
Castle ActiveRecord学习实践(7):使用HQL查询        q.SetMaxResults(_maxResults);
Castle ActiveRecord学习实践(7):使用HQL查询
Castle ActiveRecord学习实践(7):使用HQL查询 
Castle ActiveRecord学习实践(7):使用HQL查询
Castle ActiveRecord学习实践(7):使用HQL查询        
return base.GetResultsArray(typeof(Blog), q.List(), nullfalse);
Castle ActiveRecord学习实践(7):使用HQL查询
Castle ActiveRecord学习实践(7):使用HQL查询    }

Castle ActiveRecord学习实践(7):使用HQL查询
Castle ActiveRecord学习实践(7):使用HQL查询}

使用我们自定义的类
Castle ActiveRecord学习实践(7):使用HQL查询/// <summary>
Castle ActiveRecord学习实践(7):使用HQL查询
Castle ActiveRecord学习实践(7):使用HQL查询
/// 自定义查询
Castle ActiveRecord学习实践(7):使用HQL查询
Castle ActiveRecord学习实践(7):使用HQL查询
/// </summary>
Castle ActiveRecord学习实践(7):使用HQL查询
Castle ActiveRecord学习实践(7):使用HQL查询
/// <param name="authorName"></param>
Castle ActiveRecord学习实践(7):使用HQL查询
Castle ActiveRecord学习实践(7):使用HQL查询
/// <returns></returns>

Castle ActiveRecord学习实践(7):使用HQL查询
Castle ActiveRecord学习实践(7):使用HQL查询
public static Blog[] GetThreeBlogsFromAuthor( string authorName )
Castle ActiveRecord学习实践(7):使用HQL查询
Castle ActiveRecord学习实践(7):使用HQL查询
{
Castle ActiveRecord学习实践(7):使用HQL查询
Castle ActiveRecord学习实践(7):使用HQL查询    QueryWithNamedParameters q 
= new QueryWithNamedParameters();
Castle ActiveRecord学习实践(7):使用HQL查询
Castle ActiveRecord学习实践(7):使用HQL查询    q.AuthorName 
= authorName;
Castle ActiveRecord学习实践(7):使用HQL查询
Castle ActiveRecord学习实践(7):使用HQL查询    q.MaxResults 
= 3;
Castle ActiveRecord学习实践(7):使用HQL查询
Castle ActiveRecord学习实践(7):使用HQL查询    
return (Blog[]) ExecuteQuery(q);
Castle ActiveRecord学习实践(7):使用HQL查询
Castle ActiveRecord学习实践(7):使用HQL查询}

看一下简单的测试代码
Castle ActiveRecord学习实践(7):使用HQL查询[Test]
Castle ActiveRecord学习实践(7):使用HQL查询
Castle ActiveRecord学习实践(7):使用HQL查询
public void TestCustomQuery()
Castle ActiveRecord学习实践(7):使用HQL查询
Castle ActiveRecord学习实践(7):使用HQL查询
{
Castle ActiveRecord学习实践(7):使用HQL查询
Castle ActiveRecord学习实践(7):使用HQL查询    
string _StrAuthor = "Terry Lee";
Castle ActiveRecord学习实践(7):使用HQL查询
Castle ActiveRecord学习实践(7):使用HQL查询 
Castle ActiveRecord学习实践(7):使用HQL查询
Castle ActiveRecord学习实践(7):使用HQL查询    IList list 
= Blog.GetThreeBlogsFromAuthor(_StrAuthor);
Castle ActiveRecord学习实践(7):使用HQL查询
Castle ActiveRecord学习实践(7):使用HQL查询 
Castle ActiveRecord学习实践(7):使用HQL查询
Castle ActiveRecord学习实践(7):使用HQL查询    
int expectedCount = 3;
Castle ActiveRecord学习实践(7):使用HQL查询
Castle ActiveRecord学习实践(7):使用HQL查询 
Castle ActiveRecord学习实践(7):使用HQL查询
Castle ActiveRecord学习实践(7):使用HQL查询    Assert.IsNotNull(list);
Castle ActiveRecord学习实践(7):使用HQL查询
Castle ActiveRecord学习实践(7):使用HQL查询    Assert.AreEqual(expectedCount,list.Count);
Castle ActiveRecord学习实践(7):使用HQL查询
Castle ActiveRecord学习实践(7):使用HQL查询}
 
五.使用CallBack
还有一种实现方式是使用Execute()方法,这种方式与我们前面所讲的自定义查询是差不多的,只不过不用增加额外的自定义类。
Castle ActiveRecord学习实践(7):使用HQL查询[ActiveRecord("Blogs")]
Castle ActiveRecord学习实践(7):使用HQL查询
Castle ActiveRecord学习实践(7):使用HQL查询
public class Blog : ActiveRecordBase
Castle ActiveRecord学习实践(7):使用HQL查询
Castle ActiveRecord学习实践(7):使用HQL查询
{
Castle ActiveRecord学习实践(7):使用HQL查询
Castle ActiveRecord学习实践(7):使用HQL查询    
//Castle ActiveRecord学习实践(7):使用HQL查询Castle ActiveRecord学习实践(7):使用HQL查询
Castle ActiveRecord学习实践(7):使用HQL查询

Castle ActiveRecord学习实践(7):使用HQL查询    
/// <summary>
Castle ActiveRecord学习实践(7):使用HQL查询
Castle ActiveRecord学习实践(7):使用HQL查询    
/// 通过CallBack执行
Castle ActiveRecord学习实践(7):使用HQL查询
Castle ActiveRecord学习实践(7):使用HQL查询    
/// </summary>
Castle ActiveRecord学习实践(7):使用HQL查询
Castle ActiveRecord学习实践(7):使用HQL查询    
/// <param name="author"></param>
Castle ActiveRecord学习实践(7):使用HQL查询
Castle ActiveRecord学习实践(7):使用HQL查询    
/// <returns></returns>

Castle ActiveRecord学习实践(7):使用HQL查询
Castle ActiveRecord学习实践(7):使用HQL查询    
public static Blog[] GetPostsFromAuthor( string author )
Castle ActiveRecord学习实践(7):使用HQL查询
Castle ActiveRecord学习实践(7):使用HQL查询    
{
Castle ActiveRecord学习实践(7):使用HQL查询
Castle ActiveRecord学习实践(7):使用HQL查询        
return (Blog[]) Execute( typeof(Blog), new NHibernateDelegate(GetPostsFromAuthorCallback), author);
Castle ActiveRecord学习实践(7):使用HQL查询
Castle ActiveRecord学习实践(7):使用HQL查询    }

Castle ActiveRecord学习实践(7):使用HQL查询
Castle ActiveRecord学习实践(7):使用HQL查询 
Castle ActiveRecord学习实践(7):使用HQL查询
Castle ActiveRecord学习实践(7):使用HQL查询    
private static object GetPostsFromAuthorCallback(ISession session, object instance )
Castle ActiveRecord学习实践(7):使用HQL查询
Castle ActiveRecord学习实践(7):使用HQL查询    
{
Castle ActiveRecord学习实践(7):使用HQL查询
Castle ActiveRecord学习实践(7):使用HQL查询        
// 创建查询
Castle ActiveRecord学习实践(7):使用HQL查询

Castle ActiveRecord学习实践(7):使用HQL查询        IQuery query 
= session.CreateQuery( "from Blog blog where blog.Author = :author" );
Castle ActiveRecord学习实践(7):使用HQL查询
Castle ActiveRecord学习实践(7):使用HQL查询 
Castle ActiveRecord学习实践(7):使用HQL查询
Castle ActiveRecord学习实践(7):使用HQL查询        
// 设置参数
Castle ActiveRecord学习实践(7):使用HQL查询

Castle ActiveRecord学习实践(7):使用HQL查询        query.SetString(
"author", (string) instance);
Castle ActiveRecord学习实践(7):使用HQL查询
Castle ActiveRecord学习实践(7):使用HQL查询 
Castle ActiveRecord学习实践(7):使用HQL查询
Castle ActiveRecord学习实践(7):使用HQL查询        
// 获取结果
Castle ActiveRecord学习实践(7):使用HQL查询

Castle ActiveRecord学习实践(7):使用HQL查询        IList results 
= query.List();
Castle ActiveRecord学习实践(7):使用HQL查询
Castle ActiveRecord学习实践(7):使用HQL查询 
Castle ActiveRecord学习实践(7):使用HQL查询
Castle ActiveRecord学习实践(7):使用HQL查询        
// 转化结果为Array
Castle ActiveRecord学习实践(7):使用HQL查询

Castle ActiveRecord学习实践(7):使用HQL查询        Blog[] blogs 
= new Blog[results.Count];
Castle ActiveRecord学习实践(7):使用HQL查询
Castle ActiveRecord学习实践(7):使用HQL查询        results.CopyTo(blogs, 
0);
Castle ActiveRecord学习实践(7):使用HQL查询
Castle ActiveRecord学习实践(7):使用HQL查询 
Castle ActiveRecord学习实践(7):使用HQL查询
Castle ActiveRecord学习实践(7):使用HQL查询        
// 返回结果
Castle ActiveRecord学习实践(7):使用HQL查询

Castle ActiveRecord学习实践(7):使用HQL查询        
return blogs;
Castle ActiveRecord学习实践(7):使用HQL查询
Castle ActiveRecord学习实践(7):使用HQL查询    }

Castle ActiveRecord学习实践(7):使用HQL查询
Castle ActiveRecord学习实践(7):使用HQL查询}
 
编写测试代码
Castle ActiveRecord学习实践(7):使用HQL查询[Test]
Castle ActiveRecord学习实践(7):使用HQL查询
Castle ActiveRecord学习实践(7):使用HQL查询
public void TestGetPostsFromAuthor()
Castle ActiveRecord学习实践(7):使用HQL查询
Castle ActiveRecord学习实践(7):使用HQL查询
{
Castle ActiveRecord学习实践(7):使用HQL查询
Castle ActiveRecord学习实践(7):使用HQL查询    
string _StrAuthor = "Terry Lee";
Castle ActiveRecord学习实践(7):使用HQL查询
Castle ActiveRecord学习实践(7):使用HQL查询 
Castle ActiveRecord学习实践(7):使用HQL查询
Castle ActiveRecord学习实践(7):使用HQL查询    IList list 
= Blog.GetPostsFromAuthor(_StrAuthor);
Castle ActiveRecord学习实践(7):使用HQL查询
Castle ActiveRecord学习实践(7):使用HQL查询 
Castle ActiveRecord学习实践(7):使用HQL查询
Castle ActiveRecord学习实践(7):使用HQL查询    
int expectedCount = 4;
Castle ActiveRecord学习实践(7):使用HQL查询
Castle ActiveRecord学习实践(7):使用HQL查询 
Castle ActiveRecord学习实践(7):使用HQL查询
Castle ActiveRecord学习实践(7):使用HQL查询    Assert.IsNotNull(list);
Castle ActiveRecord学习实践(7):使用HQL查询
Castle ActiveRecord学习实践(7):使用HQL查询    Assert.AreEqual(expectedCount,list.Count);
Castle ActiveRecord学习实践(7):使用HQL查询
Castle ActiveRecord学习实践(7):使用HQL查询}
 
关于使用HQL查询就介绍到这儿了,相信通过HQL查询可以解决我们开发中的绝大多数的复杂查询问题。












本文转自lihuijun51CTO博客,原文链接:http://blog.51cto.com/terrylee/67665 ,如需转载请自行联系原作者